在 SharePoint 网站中获取文档库

Get document libraries in SharePoint WebSite

我需要在我的项目中使用 Sharepoint 客户端 api 并列出上传到 Sharepoint 的文件夹中的文档。我的文件夹在 link“https://mydomain.sharepoint.com/sites/blsmtekn/dyncrm/Shared%20Documents/Forms/AllItems.aspx

        using (ClientContext ctx = new ClientContext("https://mydomain.sharepoint.com/"))
        {   
            string userName = "username";
            string password = "password";

            SecureString secureString = new SecureString();
            password.ToList().ForEach(secureString.AppendChar);

            ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);

            List list = ctx.Web.Lists.GetByTitle("/Shared Documents/");
            CamlQuery caml = new CamlQuery();
            caml.ViewXml = @"<View Scope='Recursive'>
                                <Query>
                                </Query>
                            </View>";
            caml.FolderServerRelativeUrl = "/sites/blsmtekn/dyncrm/";
            ListItemCollection listItems = list.GetItems(caml);
            ctx.Load(listItems);
            ctx.ExecuteQuery();
        }

但我收到类似“列表...在 URL 的网站上不存在”这样的错误。如何递归地获取该文件夹下的文件夹和文件列表。

在我的脑海中,我看到了一些错误:您的代码声明,您的库的名称是 /Shared Documents/,而名称很可能是 Shared Documents

请更正您的 GetByTitle() 通话名称:

List list = ctx.Web.Lists.GetByTitle("Shared Documents");

第二个错误是,您网站集的 url 是错误的。应该是

ClientContext ctx = new ClientContext("https://mydomain.sharepoint.com/sites/blsmtekn/dyncrm/")

此外,您可以删除 caml.FolderServerRelativeUrl = "/sites/blsmtekn/dyncrm/";,因为那是错误的。

总而言之,您的代码应如下所示:

using (ClientContext ctx = new ClientContext("https://mydomain.sharepoint.com/sites/blsmtekn/dyncrm/"))
{   
    string userName = "username";
    string password = "password";

    SecureString secureString = new SecureString();
    password.ToList().ForEach(secureString.AppendChar);

    ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);

    List list = ctx.Web.Lists.GetByTitle("Shared Documents");
    CamlQuery caml = new CamlQuery();
    caml.ViewXml = @"<View Scope='Recursive'>
                        <Query>
                        </Query>
                    </View>";
    ListItemCollection listItems = list.GetItems(caml);
    ctx.Load(listItems);
    ctx.ExecuteQuery();
}