1

我试图使用CSOM从根站点访问数据,然后循环遍历子站点以获取存储在其列表中的文件。当我去创建ClientContext时,它可以很好地处理根URL,但是当我使用其中一个子网站的URL时,当我点击ExecuteQuery()时,会得到一个401。在子网站上查询的SharePoint 2010 CSOM-401

using (var clientContext = new ClientContext(rootURL)) 
     { 
      Console.WriteLine("Establishing connection..."); 

      var userName = System.Configuration.ConfigurationManager.AppSettings["userName"]; 
      var domain = System.Configuration.ConfigurationManager.AppSettings["domain"]; 
      var password = System.Configuration.ConfigurationManager.AppSettings["pwd"]; 
      var credentials = new NetworkCredential(userName, password, domain); 
      clientContext.Credentials = credentials; 

      _clientContext = clientContext; 

      var spContext2 = new SharePointClientDataContext(clientContext); 

      ClientContext newContext = new ClientContext(subsiteURL); 
      var allLists = newContext.Web.Lists; 
      newContext.Load(allLists); 
      newContext.ExecuteQuery(); 

      try 
      { 
... 

代码在newContext.ExecuteQuery()失败。任何想法为什么我会在子网站上遇到401,而不是根级?

还值得注意的是,这样做本地工作,但我目前正试图从我的主机操作系统运行此从我的虚拟机访问文件。

回答

2

您没有将凭据设置为新的客户端上下文,这就是为什么您会收到HTTP错误401,这意味着未经授权。在你的代码,你应该加入这一行创建newContext后:

newContext.Credentials = credentials; 

在SharePoint客户端对象模型,在不同的客户端上下文对象被完全分开。

+0

您可否详细说明“将凭据设置为新的客户端上下文”? –

+0

查看我的更新回答 – Matt

+0

就是这样,@Matt!谢谢! – awh112

相关问题