2017-06-07 71 views
0

我试图使用ClientContext(Microsoft.SharePoint.Client lib)连接到Sharepoint。Sharepoint ClientContext始终返回代码401,当我尝试连接

不幸的是,当我执行应该在Sharepoint网站上连接的代码时,我得到了401错误。

当我尝试使用网络浏览器连接时,它工作正常。

这里说到我的代码:

 using (ClientContext clientcontext = new ClientContext("http://mysite/")) 
     { 
      var credentials = new NetworkCredential(user, password, domain); 

      clientcontext.Credentials = credentials; 

      Web web = clientcontext.Web; 
      clientcontext.Load(web); 

      clientcontext.ExecuteQuery(); 
     } 

谢谢!

回答

0

使用“Microsoft.SharePoint.Client.dll”和“Microsoft.SharePoint.Client.Runtime.dll”。

using (ClientContext clientcontext = new ClientContext("http://mysite/")) 
    { 
     var credentials = new NetworkCredential(domain\UserName, password); 
     clientcontext.Credentials = credentials; 
     Web web = clientcontext.Web; 
     clientcontext.Load(web); 
     clientcontext.ExecuteQuery(); 
    } 
+0

我使用它,我也试图把“域\用户名”。这两个点都不起作用。 –

0

嗨,对不起。

请参阅下面的工作代码。 如果您是在线使用SharePoint,使用下面的代码

public static ClientContext GetClientContext(string url) 
    { 

     SecureString securePassword = new SecureString(); 
     ClientContext context = null; 
     try 
     { 
      using (context = new ClientContext(url)) 
      { 
       foreach (char c in "Password") securePassword.AppendChar(c); 
       context.Credentials = new SharePointOnlineCredentials("[email protected]", securePassword); 
       context.Load(context.Web, w => w.ServerRelativeUrl, w => w.Url); 
       context.ExecuteQuery(); 


      } 
     } 
     catch (Exception ex) 
     { 

     } 

     return context; 
    } 

如果您正在使用本地服务器上的SharePoint,你可以得到的背景下两种方式。使用应用程序池帐户或传递您的用户信誉。下面的代码使用默认的应用程序池凭据。

string parentSiteUrl = Helper.GetParentWebUrl(siteUrl); 
        clientContext = new ClientContext(parentSiteUrl); 
        clientContext.Credentials = CredentialCache.DefaultCredentials; 
        clientContext.Load(clientContext.Web, w => w.Url, w => w.Lists, w => w.ServerRelativeUrl, w => w.Title, w => w.SiteGroups);       
        clientContext.ExecuteQuery(); 

或者您可以通过您的凭据如下。

var clientContext = new ClientContext(siteUrl); 
     clientContext.Credentials = new NetworkCredential("domain\\user", "password"); 
     clientContext.Load(clientContext.Web, w => w.Lists); 
     clientContext.ExecuteQuery(); 

让我知道你是否有任何疑问。

+0

Thx @Naveen Prasath。但是,我仍然有同样的问题。我认为问题是在服务器上安装Sharepoint。 我会和照顾这台服务器的团队核实一下,如果有的话,我会让你知道这个解决方案。 –

相关问题