2013-01-23 54 views
0

我想通过代码从sharepoint文档库下载文件,因为文档库中有数千个文件。从sharepoint库下载批量文件

我想创建控制台应用程序,我将在SharePoint服务器上运行并下载文件。这种方法是否正确,还有其他一些有效的方法来做到这一点。

任何帮助代码将不胜感激。

+0

你可以做到这一点没有代码byu使用打开与资源管理器选项.. 是否有必要有一个代码? – SigarDave

回答

0

就像SigarDave说的那样,完全可以在不写一行代码的情况下实现这一点。但是,如果你真的想为这个解决方案代码,它是这样的:

static void Main(string[] args) 
{ 
    // Change to the URL of your site 
    using (var site = new SPSite("http://MySite")) 
    using (var web = site.OpenWeb()) 
    { 
    var list = web.Lists["MyDocumentLibrary"]; // Get the library 
    foreach (SPListItem item in list.Items) 
    { 
     if (item.File != null) 
     { 
     // Concat strings to get the absolute URL 
     // to pass to an WebClient object. 
     var fileUrl = string.Format("{0}/{1}", site.Url, item.File.Url);    
     var result = DownloadFile(fileUrl, "C:\\FilesFromMyLibrary\\", item.File.Name); 
     Console.WriteLine(result ? "Downloaded \"{0}\"" : "Error on \"{0}\"", item.File.Name); 
     } 
    } 
    } 
    Console.ReadKey(); 
} 

private static bool DownloadFile(string url, string dest, string fileName) 
{ 
    var client = new WebClient(); 

    // Change the credentials to the user that has the necessary permissions on the 
    // library 
    client.Credentials = new NetworkCredential("Username", "Password", "Domain"); 
    var bytes = client.DownloadData(url); 

    try 
    { 
    using (var file = File.Create(dest + fileName)) 
    { 
     file.Write(bytes, 0, bytes.Length); // Write file to disk 
     return true; 
    } 
    } 
    catch (Exception) 
    { 
    return false; 
    } 
} 
0

另一种方式,而无需使用任何脚本是通过打开使用IE的文档库,然后在功能区,你可以在文件浏览器,其中点击打开您可以将文件拖放到桌面上!