从sharepoint列表中下载项目

时间:2018-06-05 07:11:49

标签: c# list sharepoint

我必须从sharepoint列表中获取一些文件(www.domain.sharepoint.com/site/appname/List/ListName)

我可以将目标链接反序列化到列表中的每个项目,但我无法通过链接下载这些项目。

如何下​​载列表中存储的文件?

谢谢

1 个答案:

答案 0 :(得分:0)

请使用GetFileByServativeUrl获取文件对象然后下载文件流,这里有一个代码片段供您参考:

 using (var clientContext = new ClientContext("https://xxx.sharepoint.com/sites/xxx"))
            {
                clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
                Web web = clientContext.Web;
                clientContext.Load(web, a => a.ServerRelativeUrl);
                clientContext.ExecuteQuery();


                Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByServerRelativeUrl("/sites/jerrydev/Shared%20Documents/test.png");
                clientContext.Load(file);
                clientContext.ExecuteQuery();
                if (file != null)
                {
                    FileInformation fileInfo = File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
                    clientContext.ExecuteQuery();

                    var folderpath = @"c:\downloadfolder";
                    if (!System.IO.Directory.Exists(folderpath))
                    {
                        System.IO.Directory.CreateDirectory(folderpath);
                    }
                    using (var fileStream = new System.IO.FileStream(folderpath + @"\" + file.Name, System.IO.FileMode.Create))
                    {
                        fileInfo.Stream.CopyTo(fileStream);
                    }
                }
}

enter image description here