首页 > 解决方案 > 在 C# 代码中使用 Sharepoint 365 现代体验获取运行时错误

问题描述

我正在尝试访问 SharePoint 网站中的列表,我正在使用具有现代体验的 Sharepoint 365。

我的问题是我上线超时错误

clientContext.ExecuteQuery();

我将此链接用作参考: https ://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code#basic-operations -with-the-sharepoint-net-client-object-model

这是我的代码:

using (ClientContext clientContext = new ClientContext("https:siteurl"))
{
    SecureString passWord = new SecureString();

    foreach (char c in "mypwd".ToCharArray()) passWord.AppendChar(c);

    clientContext.Credentials = new SharePointOnlineCredentials("myuser", passWord);

    Web web = clientContext.Web;

    // Retrieve all lists from the server.
    // For each list, retrieve Title and Id.
    clientContext.Load(web.Lists,
                         lists => lists.Include(list => list.Title,
                                                list => list.Id));
           
    // Execute query.
    clientContext.ExecuteQuery();

    // Enumerate the web.Lists.
    foreach (List list in web.Lists)
    {
        label1.Text = label1.Text + ", " + list.Title;
    }
}

标签: c#sharepointsharepoint-onlinesharepoint-list

解决方案


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

using (ClientContext clientContext = new ClientContext("https:siteurl"))
{
    SecureString passWord = new SecureString();

    foreach (char c in "mypwd".ToCharArray()) passWord.AppendChar(c);

    clientContext.Credentials = new SharePointOnlineCredentials("myuser", passWord);   
    clientContext.RequestTimeout = -1;
    Web web = clientContext.Web;

    // Retrieve all lists from the server.
    // For each list, retrieve Title and Id.
    clientContext.Load(web.Lists,
                         lists => lists.Include(list => list.Title,
                                                list => list.Id));
           
    // Execute query.
    clientContext.ExecuteQuery();

    // Enumerate the web.Lists.
    foreach (List list in web.Lists)
    {
        label1.Text = label1.Text + ", " + list.Title;
    }
}

推荐阅读