[英]Calling C# code from JavaScript in SharePoint
Ok here's what I'm trying to do. 好的,这就是我想要做的。
I have this custom action (button on my SharePoint-ribbon). 我有此自定义操作(我的SharePoint功能区上的按钮)。 This should call a Javascript, which in turn should call a C#-code.
这应该调用Javascript,而Javascript又应该调用C#代码。
I have the following: 我有以下几点:
<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>
And i have a 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);
}
}
}
It seems my code gets executed, but I cannot find my file anywhere. 似乎我的代码被执行了,但是我找不到文件。 What am I missing?
我想念什么?
you can use __DoPostback to invoke a server side hit from javascript. 您可以使用__DoPostback从javascript调用服务器端匹配。
<script type="text/javascript">
function ServerPostWithParameter(parameter)
{
__doPostBack('btnSave', parameter)
}
</script>
in server side, 在服务器端,
public void Page_Load(object sender, EventArgs e)
{
string parameter = Request["__EVENTARGUMENT"]; // this is your parameters
// Request["__EVENTTARGET"]; // this is your button
}
You can just create an HttpHandler with your server-side code and call it with parameters from JavaScript. 您可以只使用服务器端代码创建HttpHandler,然后使用JavaScript中的参数进行调用。
Eg create an ~sitecollection/_layouts/15/MyCustomHandler.ashx and call it from JavaScript like this (SharePoint 2013 uses virtual path to layouts directory as '_layouts/15', SharePoint 2010 -- just '_layouts'): 例如,创建〜sitecollection / _layouts / 15 / MyCustomHandler.ashx并通过JavaScript进行如下调用(SharePoint 2013使用虚拟路径将布局目录设置为“ _layouts / 15”,SharePoint 2010-仅“ _layouts”):
$.get(_spPageContextInfo.siteServerRelativeUrl + '/_layouts/15/MyCustomHandler.ashx?Param1=Value1&Param2=Value2');
I've solved it as follows : 我已经解决了如下问题:
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.