1
votes

"Operation has timed out" error while connecting to Office 365 Sharepoint from asp.net web application

I have tried finding answers and implementing solutions like below:

How to connect to SharePoint on Office 365 with CSOM from C#?

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.

2

2 Answers

1
votes

After a lot of search I realized that we need proxy to connect to Sharepoint Online and implemented following code to achieve

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

0
votes

Add clientContext.RequestTimeout = -1 in the code, the code below for your reference.

string siteUrl = "https://tenant.sharepoint.com/sites/lz";
string userName = "[email protected]";
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();
}