[英]The remote server returned an error: (401) Unauthorized in context.ExecuteQuery() : ASP.Net
我正在使用以下代碼段連接Sharepoint Server API。 此片段在空白項目中運行良好,符合預期。 它返回列表公告,共享文檔,任務...等。
但是當我將它與現有應用程序集成時,它會拋出The remote server returned an error: (401) Unauthorized
context.ExecuteQuery()
The remote server returned an error: (401) Unauthorized
錯誤消息。
我嘗試提供Context.Credentials = new NetworkCredential( username, password, domain );
返回相同的錯誤。
碼:
using (ClientContext context = new ClientContext(ServerUrl))
{
//get all the lists from sharepoint
//all the files and folders resides inside some list.
Web web = context.Web;
context.Load(web.Lists, its => its.Include(it => it.Title, it => it.Id, it => it.RootFolder.ServerRelativeUrl));
context.ExecuteQuery();
foreach (var list in web.Lists)
{
//add all the lists to treeview
//list id is assigned to node value and list's server relative url is assigned to tooltip
tvItems.Nodes.Add(new Telerik.Web.UI.RadTreeNode
{
Value = list.Id.ToString(),
Text = list.Title,
ExpandMode = Telerik.Web.UI.TreeNodeExpandMode.ServerSide,
ToolTip = list.RootFolder.ServerRelativeUrl
});
}
}
我嘗試更改context.Load(web.Lists, its => its.Include(...);
代碼行到context.Load(web.Lists, its => ClientObjectQueryableExtension.Include(...));
make確保sharepoint參考不會與現有的一次混合。
任何幫助都將很好地解決這個問題。
如果您想要從提供商托管的應用程序中訪問上下文,您可以使用以下內容:
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.