2015-03-13 88 views
0

好吧,這就是我想要做的。從SharePoint中的JavaScript調用C#代碼

我有這個自定義操作(我的SharePoint功能區上的按鈕)。這應該調用一個Javascript,它反過來應該調用一個C#代碼。

我有以下幾點:

<CustomAction 
Id="Ribbon.Documents.DocsetZip" 
Title="Download Document Set as ZIP" 
RegistrationType="ContentType" 
RegistrationId="0x0120D520" 
Location="CommandUI.Ribbon" 
> 
<CommandUIExtension> 
    <CommandUIDefinitions> 
    <CommandUIDefinition 
     Location="Ribbon.Documents.Share.Controls._children"> 
     <Button Id="Ribbon.Document.Share.DownasZip" 
         Sequence="20" 
         Command="Ribbon.ManageDocumentSet.MDS.Manage.DownZip" 
         Alt="Download as ZIP" 
         Image16by16="/_layouts/images/zipfile16x.png" 
         Image32by32="/_layouts/images/zipfile32x.png" 
         LabelText="Download as ZIP file" 
     ToolTipTitle="Download as ZIP file" 
     ToolTipDescription="Compress the document set and download" 
     TemplateAlias="o1"/> 
    </CommandUIDefinition> 
    </CommandUIDefinitions> 
    <CommandUIHandlers> 
    <CommandUIHandler 
     Command="Ribbon.ManageDocumentSet.MDS.Manage.DownZip" 
     CommandAction="javascript:__doPostBack('DownloadZipDelegateEvent', '')" /> 
    </CommandUIHandlers> 
</CommandUIExtension> 

,我有一個類:

public class MyRibbonDelegateClass : WebControl 
{ 

    protected override void OnLoad(EventArgs e) 
    { 
     this.EnsureChildControls(); 
     base.OnLoad(e); 
     if (this.Page.Request["__EVENTTARGET"] == "DownloadZipDelegateEvent") 
     { 
      using (TextWriter writer = File.CreateText("C:\\temp\\perl.txt")) 
      { 
       // 
       // Write one line. 
       // 
       writer.WriteLine("First line"); 
       // 
       // Write two strings. 
       // 
       writer.Write("A "); 
       writer.Write("B "); 
       // 
       // Write the default newline. 
       // 
       writer.Write(writer.NewLine); 
      } 

     } 
    } 

看來我的代碼得到執行,但我找不到我的文件。 我錯過了什麼?

回答

0

我如下解決它:

function getOutlook() { 
    var xmlHttpReq = createXMLHttpRequest(); 
    xmlHttpReq.open("GET", _spPageContextInfo.siteServerRelativeUrl + "/_layouts/SendDocuments/MyCustomHandler.ashx?ItemsArray=" + fileRefArray, false); 
     xmlHttpReq.send(null); 
} 

function createXMLHttpRequest() { 
    try { return new XMLHttpRequest(); } catch (e) { } 
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { } 
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { } 
    alert("XMLHttpRequest not supported"); 
    return null; 
} 
0

您可以使用__DoPostback從javascript調用服務器端命中。

<script type="text/javascript"> 
function ServerPostWithParameter(parameter) 
{ 
    __doPostBack('btnSave', parameter) 
} 
</script> 
在服務器端

public void Page_Load(object sender, EventArgs e) 
{ 
    string parameter = Request["__EVENTARGUMENT"]; // this is your parameters 
    // Request["__EVENTTARGET"]; // this is your button 
} 
+0

但我實際上應該從上一個按鈕,我加入自己的SharePoint的帶狀點擊調用C#。所以不是從我自己創建的頁面開始的。 – 2015-03-13 13:40:24

0

您可以只創建一個HttpHandler與服務器端代碼,並與JavaScript的參數調用它。

E.g.創建〜sitecollection/_layouts/15/MyCustomHandler.ashx並像這樣從JavaScript調用它(SharePoint 2013將佈局目錄的虛擬路徑用作'_layouts/15',SharePoint 2010 - 僅'_layouts'):

$.get(_spPageContextInfo.siteServerRelativeUrl + '/_layouts/15/MyCustomHandler.ashx?Param1=Value1&Param2=Value2'); 
+0

我如何確保ashx得到部署?出於某種原因,它沒有得到部署。並且deploymenttype設置爲NoDeployment。我該如何改變它? – 2015-03-23 09:28:49

+0

好吧,我發現這篇文章:好的,我用它來部署它:http://www.lifeonplanetgroove.com/adding-and-deploying-generic-handlers-ashx-to-a-sharepoint-2010-visual-studio-項目/ 但我仍然沒有運行我的代碼。 – 2015-03-23 10:07:06

+0

我有這在我的js文件中: 函數SendSingleDoc(ListId,ItemId){ var sdList; try {_spPageContextInfo.siteServerRelativeUrl +'/_layouts/SendDocuments/MyCustomHandler.ashx'); } }但我總是收到一個錯誤:'$'未定義 – 2015-03-23 12:35:02