繁体   English   中英

在SharePoint中从JavaScript调用C#代码

[英]Calling C# code from JavaScript in SharePoint

好的,这就是我想要做的。

我有此自定义操作(我的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM