C#.Net将SharePoint Online文件夹的内容下载到网络驱动器

时间:2019-07-02 04:04:10

标签: c# .net sharepoint sharepoint-online

我有以下代码:

ClientContext context = new ClientContext("https://Website.sharepoint.com/sites/Subsite");
            context.Credentials = new SharePointOnlineCredentials(Username, GetPasswordFromConsoleInput(Password));

            context.Load(context.Web);
            List list = context.Web.Lists.GetByTitle("Documents");
            context.ExecuteQuery();
            CamlQuery query = new CamlQuery();
            query.FolderServerRelativeUrl = "Documents/Folder1/Folder2/Folder3";


            query.ViewXml = @"<View Scope='Recursive'>
                                <Query>
                                </Query>
                            </View>";

            ListItemCollection folderItems = list.GetItems(query);
            context.Load(folderItems);
            context.ExecuteQuery();

            foreach (ListItem li in folderItems)
            {
                Microsoft.SharePoint.Client.File file = li.File;
                if (file != null)
                {
                    //how to download all files
                }
            }
//How to download one single file in the root "Documents" folder with specific name

1)我需要它连接到SharePoint365网站并下载子站点库中特定文件夹中的所有文件。这些文件位于文档库的第3个文件夹中。文件应转到网络位置\\server\d$\files

2)我需要它执行相同的操作,但对于文档库的根位置中只有一个具有特定名称的单个文件。

现在它给了我这个错误:'The 'query.FolderServerRelativeUrl' argument is invalid.' 谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

示例代码供您参考。

string targetSiteURL = @"https://tenant.sharepoint.com/sites/lee";

            var login = "user@tenant.onmicrosoft.com";
            var password = "password";

            var securePassword = new SecureString();

            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            using (ClientContext clientContext = new ClientContext(targetSiteURL))
            {
                clientContext.Credentials = onlineCredentials;

                Web web = clientContext.Web;
                clientContext.Load(web);                
                clientContext.Load(web, wb => wb.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                List list = web.Lists.GetByTitle("mydoc3");
                clientContext.Load(list);                

                Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/mydoc3/ParentFolder/");
                clientContext.Load(folder);
                clientContext.ExecuteQuery();

                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = @"<View Scope='Recursive'>
                                     <Query>
                                     </Query>
                                 </View>";
                camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
                ListItemCollection listItems = list.GetItems(camlQuery);
                clientContext.Load(listItems);
                clientContext.ExecuteQuery();
                foreach(var item in listItems)
                {
                    var file = item.File;
                    clientContext.Load(file);
                    clientContext.ExecuteQuery();
                    if (file != null)
                    {
                        Console.WriteLine(file.Name);
                    }
                }
            }
            Console.ReadKey();