嗨,對不起。
請參閱下面的工作代碼。 如果您是在線使用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();
讓我知道你是否有任何疑問。
我使用它,我也試圖把「域\用戶名」。這兩個點都不起作用。 –