如何使用 C# 在 Office 365 上连接到 SharePoint 时修复'operation has timed out'



从asp.net web应用程序连接到Office 365 Sharepoint时出现"操作超时"错误

我试着找到答案并实施如下解决方案:

如何使用CSOM从C#连接到Office 365上的SharePoint?

此外,一些博客建议进行异步查询,这样既不会抛出错误,也不会给出任何结果。还尝试在没有任何帮助的情况下设置超时属性。

以下是我的代码:

SharePointOnlineCredentials networkCredential = new 
SharePointOnlineCredentials(SharePointUser, SharePointPassword);
Context = new ClientContext(SharePointURL);
Context.Credentials = networkCredential;
Web = Context.Web;
Context.Load(Web);
Context.ExecuteQuery();`

此外,奇怪的是,我能够使用Console应用程序连接和获取数据,但我需要在web应用程序中实现这一点。

经过大量搜索,我意识到我们需要代理才能连接到Sharepoint Online,并实现了以下代码来实现

clientContext.ExecutingWebRequest+=(s,e(=>{e.WebRequestExecutior.WebRequest.Proxy.Credentials=System.Net.CredentialCache.DefaultCredentials;};

在代码中添加clientContext.RequestTimeout=-1,下面的代码供您参考。

string siteUrl = "https://tenant.sharepoint.com/sites/lz";
string userName = "lz@tenant.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
clientContext.RequestTimeout = -1;
var web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
}

相关内容

最新更新