C# 调用Web方法时SPContext为null
我有一个SharePoint版面页面,其中包含此web方法C# 调用Web方法时SPContext为null,c#,sharepoint,sharepoint-2013,C#,Sharepoint,Sharepoint 2013,我有一个SharePoint版面页面,其中包含此web方法 [WebMethod(EnableSession = false)] [ScriptMethod] public static List<ItemData> GetItems(string SearchTerm) { SPSecurity.RunWithElevatedPrivileges(delegate() {
[WebMethod(EnableSession = false)]
[ScriptMethod]
public static List<ItemData> GetItems(string SearchTerm)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.RootWeb)
{
SPList list = web.Lists.TryGetList("Data");
_documentListId = list.ID;
}
}
});
}
问题是,当通过此ajax代码调用web方法时,SPContext将显示为null。如果我在SPContext上放置一个断点并查看它的值,那么它会以某种方式检索并填充它,但不会没有断点
有人能帮我解决这个问题吗
谢谢我不知道它为什么这么做,但是如果我将SpContext.Current移到SPSecurity.RunWithElevatedPrivileges(delegate())之外编码,这样我每次都可以加载上下文,这就解决了我的问题。当您使用“RunWithElevatedPrivilegs”时,您实际上创建了一个新上下文,因此当前上下文在调用中无效
$.ajax({
type: "POST",
url: "multispr.aspx/GetItems",
data: JSON.stringify({ SearchTerm: $('#SearchTerm').val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data, status) {
self.Items(data.d);
myApp.hidePleaseWait();
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});