我们可以在Provider托管应用中使用Web方法吗? [英] Can we Use Web Methods in Provider hosted app
本文介绍了我们可以在Provider托管应用中使用Web方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为Azure托管的SharePoint Online开发提供商托管应用程序(Web窗体)。
解决方案
你可以试试这个:
aspx页面:
<%@ Page Language =" C#" AutoEventWireup = QUOT;真"代码隐藏= QUOT; Default.aspx.cs"继承= QUOT; PHWebMethodWeb.Default" %GT;
<!DOCTYPE html PUBLIC" - // W3C // DTD XHTML 1.0 Transitional // EN" " HTTP://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns =" http://www.w3.org/1999/xhtml">
< head runat =" server">
< title>< / title>
< script type =" text / javascript" SRC =" HTTPS://code.jquery.com/jquery-1.12.4.js">< /脚本>
< script type =" text / javascript">
(document).ready(function(){
( '#btn')。click(function(){
jQuery.ajax({
url:'default.aspx / GetData',
type:" POST",
dataType:" json",
contentType:" application / json; charset = utf-8",
success:function(data){
alert(JSON.stringify(data)) ;
}
});
});
});
< / script>
< / head>
< body>
< form id =" form1" RUNAT = QUOT;服务器">
< div>
< input type =" button"名称= QUOT; BTN1" ID = QUOT; BTN"值= QUOT; ClickMe" />
< / div>
< / form>
< / body>
< / html>
后端 :
public partial class默认值:System.Web.UI.Page
{
protected void Page_PreInit(object sender,EventArgs e)
{
Uri redirectUrl ;
switch(SharePointContextProvider.CheckRedirectionStatus(Context,out redirectUrl))
{
case RedirectionStatus.Ok:
return;
case RedirectionStatus.ShouldRedirect:
Response.Redirect(redirectUrl.AbsoluteUri,endResponse:true);
休息;
case RedirectionStatus.CanNotRedirect:
Response.Write("处理您的请求时出错。");
Response.End();
休息;
}
}
protected static SharePointContext _Context = null;
[WebMethod()]
公共静态字符串GetData()
{
if(_Context == null)
{
return" ;上下文未被启动";
}
else
{
using(var clientContext = _Context.CreateUserClientContextForSPHost())
{
clientContext.Load(clientContext.Web,web => ; web.Title);
clientContext.ExecuteQuery();
返回clientContext.Web.Title;
}
}
}
protected void Page_Load(object sender,EventArgs e)
{
//以下代码获取客户端使用TokenHelper的context和Title属性。
//要访问其他属性,应用可能需要在主机网络上请求权限。
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
_Context = spContext;
}
}
< span style ="color:#1f497d">
最好的问候,
利跨度>
I am developing a provider hosted app (Web Forms) for SharePoint Online hosted in Azure.
解决方案
Hi,
You may try this:
aspx page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PHWebMethodWeb.Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script> <script type="text/javascript">
(document).ready(function () {
('#btn').click(function () { jQuery.ajax({ url: 'default.aspx/GetData', type: "POST", dataType: "json", contentType: "application/json; charset=utf-8", success: function (data) { alert(JSON.stringify(data)); } }); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <input type="button" name="btn1" id="btn" value="ClickMe" /> </div> </form> </body> </html>
Back-end:
public partial class Default : System.Web.UI.Page { protected void Page_PreInit(object sender, EventArgs e) { Uri redirectUrl; switch (SharePointContextProvider.CheckRedirectionStatus(Context, out redirectUrl)) { case RedirectionStatus.Ok: return; case RedirectionStatus.ShouldRedirect: Response.Redirect(redirectUrl.AbsoluteUri, endResponse: true); break; case RedirectionStatus.CanNotRedirect: Response.Write("An error occurred while processing your request."); Response.End(); break; } } protected static SharePointContext _Context = null; [WebMethod()] public static string GetData() { if (_Context == null) { return "Context not initilized"; } else { using (var clientContext = _Context.CreateUserClientContextForSPHost()) { clientContext.Load(clientContext.Web, web => web.Title); clientContext.ExecuteQuery(); return clientContext.Web.Title; } } } protected void Page_Load(object sender, EventArgs e) { // The following code gets the client context and Title property by using TokenHelper. // To access other properties, the app may need to request permissions on the host web. var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); _Context = spContext; } }
Best Regards,
Lee
这篇关于我们可以在Provider托管应用中使用Web方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文