我正在使用以下代码段来连接sharepoint服务器API。此片段在空白项目中运行良好,符合预期。它返回列表公告,共享文档,任务......等。
但是当我将其与现有应用程序集成时,会在The remote server returned an error: (401) Unauthorized
中抛出context.ExecuteQuery()
错误消息。
我尝试提供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(...));
,以确保共享点引用不会与现有代码混合。
任何帮助都可以解决问题。
答案 0 :(得分:0)
如果您想从提供商托管的应用内访问上下文,可以使用以下内容:
var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
....
}