2013-01-23 55 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的文檔庫,然後在功能區,你可以在文件瀏覽器,其中點擊打開您可以將文件拖放到桌面上!