在 SharePoint 中从 JavaScript 调用 C# 代码

Calling C# code from JavaScript in SharePoint

本文关键字:代码 调用 JavaScript SharePoint 中从      更新时间:2023-09-26

好的,这就是我要做的。

我有此自定义操作(我的 SharePoint 功能区上的按钮)。这应该调用Javascript,而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);
            }
        }
    }

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

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