2009-01-22 118 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); 
    }