在SharePoint WebSite中获取文档库

时间:2016-10-10 11:42:46

标签: c# sharepoint sharepoint-2010 sharepoint-clientobject

我需要在我的项目中使用sharepoint客户端api,并将文件列在上传到sharepoint的文件夹中。我的文件夹位于“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”。如何递归获取该文件夹下的文件夹和文件列表。

1 个答案:

答案 0 :(得分:0)

从头到尾,我发现了一些错误:您的代码指出,您的图书馆名称为/Shared Documents/,而名称最有可能是Shared Documents

请修正GetByTitle()来电的姓名:

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

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

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();
}