2009-01-22 89 views

回答

58

您可以使用對象模型或SharePoint Webservices將文檔上載到SharePoint庫。

上傳使用對象模型:

String fileToUpload = @"C:\YourFile.txt"; 
String sharePointSite = "http://yoursite.com/sites/Research/"; 
String documentLibraryName = "Shared Documents"; 

using (SPSite oSite = new SPSite(sharePointSite)) 
{ 
    using (SPWeb oWeb = oSite.OpenWeb()) 
    { 
     if (!System.IO.File.Exists(fileToUpload)) 
      throw new FileNotFoundException("File not found.", fileToUpload);      

     SPFolder myLibrary = oWeb.Folders[documentLibraryName]; 

     // Prepare to upload 
     Boolean replaceExistingFiles = true; 
     String fileName = System.IO.Path.GetFileName(fileToUpload); 
     FileStream fileStream = File.OpenRead(fileToUpload); 

     // Upload document 
     SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles); 

     // Commit 
     myLibrary.Update(); 
    } 
} 
+2

Chadworthington,SPSite的是命名空間Microsoft.SharePoint程序的一部分,所以你需要添加引用Microsoft.SharePoint.dll的。 假設你正在開發服務器上,可以在這裏找到DLL:C:\ Program Files \ Common Files \ Microsoft Shared \ Web服務器擴展\ 12 \ ISAPI \ Microsoft.SharePoint.dll – 2009-06-04 09:23:46

+1

等待一段時間...此代碼只會在加入農場的箱子裏工作,對嗎?在任何其他框中,它需要使用http://msdn.microsoft.com/en-us/library/ee857094.aspx – Ariel 2011-05-05 02:35:34

+0

@Ariel你是對的。客戶端API是從外部訪問SharePoint服務器的方式。另請參閱http://msdn.microsoft.com/en-us/library/ee537564.aspx – 2011-08-05 16:19:05

5

作爲替代web服務,您可以使用從FrontPage RPC API的調用put document。這還具有使您能夠在與文件數據相同的請求中提供元數據(列)的額外好處。顯而易見的缺點是該協議有點晦澀(與非常好的Web服務相比)。

有關解釋使用Frontpage RPC的參考應用程序,請參閱CodePlex上的SharePad項目。

7
string filePath = @"C:\styles\MyStyles.css"; 
    string siteURL = "http://MyDomain.net/"; 
    string libraryName = "Style Library"; 

    using (SPSite oSite = new SPSite(siteURL)) 
    { 
     using (SPWeb oWeb = oSite.OpenWeb()) 
     { 
      if (!System.IO.File.Exists(filePath)) 
       throw new FileNotFoundException("File not found.", filePath);      

      SPFolder libFolder = oWeb.Folders[libraryName]; 

      // Prepare to upload 
      string fileName = System.IO.Path.GetFileName(filePath); 
      FileStream fileStream = File.OpenRead(filePath); 

      //Check the existing File out if the Library Requires CheckOut 
      if (libFolder.RequiresCheckout) 
      { 
       try { 
        SPFile fileOld = libFolder.Files[fileName]; 
        fileOld.CheckOut(); 
       } catch {} 
      } 

      // Upload document 
      SPFile spfile = libFolder.Files.Add(fileName, fileStream, true); 

      // Commit 
      myLibrary.Update(); 

      //Check the File in and Publish a Major Version 
      if (libFolder.RequiresCheckout) 
      { 
        spFile.CheckIn("Upload Comment", SPCheckinType.MajorCheckIn); 
        spFile.Publish("Publish Comment"); 
      } 
     } 
    } 
11

,如果你得到這個錯誤 「值不在預期範圍內」 在這一行:

SPFolder myLibrary = oWeb.Folders[documentLibraryName]; 

改用這種修正這個錯誤:

SPFolder myLibrary = oWeb.GetList(URL OR NAME).RootFolder; 

因爲它們是唯一的,所以始終使用URl來獲取列表或其他名稱,因爲名稱不是最好的方式;)

3

使用SharePoint 2013的新庫,我好不容易纔做這樣的事情:

private void UploadToSharePoint(string p, out string newUrl) //p is path to file to load 
    { 
     string siteUrl = "https://myCompany.sharepoint.com/site/"; 
     //Insert Credentials 
     ClientContext context = new ClientContext(siteUrl); 

     SecureString passWord = new SecureString(); 
     foreach (var c in "mypassword") passWord.AppendChar(c); 
     context.Credentials = new SharePointOnlineCredentials("myUserName", passWord); 
     Web site = context.Web; 

     //Get the required RootFolder 
     string barRootFolderRelativeUrl = "Shared Documents/foo/bar"; 
     Folder barFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl); 

     //Create new subFolder to load files into 
     string newFolderName = baseName + DateTime.Now.ToString("yyyyMMddHHmm"); 
     barFolder.Folders.Add(newFolderName); 
     barFolder.Update(); 

     //Add file to new Folder 
     Folder currentRunFolder = site.GetFolderByServerRelativeUrl(barRootFolderRelativeUrl + "/" + newFolderName); 
     FileCreationInformation newFile = new FileCreationInformation { Content = System.IO.File.ReadAllBytes(@p), Url = Path.GetFileName(@p), Overwrite = true }; 
     currentRunFolder.Files.Add(newFile); 
     currentRunFolder.Update(); 

     context.ExecuteQuery(); 

     //Return the URL of the new uploaded file 
     newUrl = siteUrl + barRootFolderRelativeUrl + "/" + newFolderName + "/" + Path.GetFileName(@p); 
    } 
相關問題