简体   繁体   English

如何在使用 C# 连接到 Office 365 上的 sharepoint 时修复“操作已超时”

[英]How to fix 'operation has timed out' while connecting to sharepoint on Office 365 using C#

"Operation has timed out" error while connecting to Office 365 Sharepoint from asp.net web application从 asp.net web 应用程序连接到 Office 365 Sharepoint 时出现“操作超时”错误

I have tried finding answers and implementing solutions like below:我尝试寻找答案并实施如下解决方案:

How to connect to SharePoint on Office 365 with CSOM from C#? 如何使用 C# 的 CSOM 连接到 Office 365 上的 SharePoint?

Also some blogs suggested making an asynchronous query, which does not throw error but also does not give any results.还有一些博客建议进行异步查询,它不会抛出错误,但也不会给出任何结果。 Also tried setting timeout property without any help.还尝试在没有任何帮助的情况下设置超时属性。

Below is my code:下面是我的代码:

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

Also, strangely I am able to connect and get data using Console application, but I need to get this working in web application.另外,奇怪的是我能够使用控制台应用程序连接和获取数据,但我需要在 web 应用程序中让它工作。

After a lot of search I realized that we need proxy to connect to Sharepoint Online and implemented following code to achieve经过大量搜索,我意识到我们需要代理连接到 Sharepoint Online 并实现以下代码来实现

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

Add clientContext.RequestTimeout = -1 in the code, the code below for your reference.在代码中添加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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM