在 SharePoint 中从 JavaScript 调用 C# 代码

Calling C# code from JavaScript in SharePoint

好的,这就是我正在尝试做的事情。

我有这个自定义操作(我的 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>

我有一个 class:

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);
            }

        }
    }

我的代码似乎被执行了,但我无法在任何地方找到我的文件。 我错过了什么?

您可以使用 __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
}

您只需使用服务器端代码创建一个 HttpHandler,然后使用来自 JavaScript 的参数调用它。

例如创建一个 ~sitecollection/_layouts/15/MyCustomHandler.ashx 并从 JavaScript 调用它(SharePoint 2013 使用布局目录的虚拟路径作为 '_layouts/15',SharePoint 2010 - 只是 '_layouts'):

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

我是这样解决的:

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;
}