2008-08-28 99 views
31

通過WSS 3.0版本的內置Web服務將文件上載到SharePoint服務器上的文檔庫的最佳方式是什麼?通過內置的web服務將文件上傳到SharePoint

繼兩個初始答案...

  • 我們一定要使用Web服務層,我們將作出從遠程客戶端應用程序這些調用。

  • WebDAV方法適用於我們,但我們希望與Web服務集成方法保持一致。

有另外一個Web服務來上傳文件,痛苦的,但作品的所有時間。

您是否指「複製」服務? 我們已經成功使用此服務的CopyIntoItems方法。這是僅使用WSS Web服務API將文件上傳到文檔庫的推薦方式嗎?

我已經發布我們的代碼作爲建議的答案。

回答

17
使用WSS「複製」 Web服務將文檔上傳到庫

例...

public static void UploadFile2007(string destinationUrl, byte[] fileData) 
{ 
    // List of desination Urls, Just one in this example. 
    string[] destinationUrls = { Uri.EscapeUriString(destinationUrl) }; 

    // Empty Field Information. This can be populated but not for this example. 
    SharePoint2007CopyService.FieldInformation information = new 
     SharePoint2007CopyService.FieldInformation(); 
    SharePoint2007CopyService.FieldInformation[] info = { information }; 

    // To receive the result Xml. 
    SharePoint2007CopyService.CopyResult[] result; 

    // Create the Copy web service instance configured from the web.config file. 
    SharePoint2007CopyService.CopySoapClient 
    CopyService2007 = new CopySoapClient("CopySoap"); 
    CopyService2007.ClientCredentials.Windows.ClientCredential = 
     CredentialCache.DefaultNetworkCredentials; 
    CopyService2007.ClientCredentials.Windows.AllowedImpersonationLevel = 
     System.Security.Principal.TokenImpersonationLevel.Delegation; 

    CopyService2007.CopyIntoItems(destinationUrl, destinationUrls, info, fileData, out result); 

    if (result[0].ErrorCode != SharePoint2007CopyService.CopyErrorCode.Success) 
    { 
     // ... 
    } 
} 
+0

當文件已經存在時,是否發現有錯誤的問題?我開始使用這種技術,但發現我經常收到錯誤「無法在請求的目的地創建項目,請確認文件夾存在並且您有權編輯它」。還有其他人有這個問題嗎? – misteraidan 2009-07-01 00:05:58

1

從在工作運動課:

偷懶的辦法:你的Windows WebDAV的文件系統接口。由於它依賴於操作系統上運行的WindowsClient服務,並且只能在運行在端口80上的網站上運行,所以它作爲程序化解決方案是不好的。將驅動器映射到文檔庫並獲取文件複製。

還有一個web服務來上傳文件,痛苦但始終工作。

我相信你可以通過FrontPage API上傳文件,但我不知道任何人使用它。

+0

Word,Excel(和其他Office應用程序)使用FrontPage API的方式與其他許多方式一樣。在2010年的版本中,CMIS將得到支持。 – 2009-05-21 15:34:58

1

不確定要使用哪個Web服務,但如果您處於可以使用SharePoint .NET API Dll的位置,那麼使用SPList和SPLibrary.Items.Add非常簡單。

+0

不知道爲什麼這是高效的: 「我們肯定需要使用Web服務層,因爲我們將從遠程客戶端應用程序進行這些調用。」 – 2016-06-24 16:00:39

+0

@MichaelBlackburn因爲如果你看看這個問題的歷史,直到我發佈我的答案之後纔將這個澄清添加到問題中。 – 2016-11-16 04:56:20

+0

啊,我的道歉。 – 2016-11-16 12:46:09

9

另一種選擇是使用純醇」 HTTP PUT:

WebClient webclient = new WebClient(); 
webclient.Credentials = new NetworkCredential(_userName, _password, _domain); 
webclient.UploadFile(remoteFileURL, "PUT", FilePath); 
webclient.Dispose(); 

凡remoteFileURL點到SharePoint文檔庫...

8

有幾件事情要考慮:

  • Copy.CopyIntoItems 需要的文件成爲已經存在一些服務器 。該文檔作爲web服務調用的參數傳遞,這將限制文檔的大小。 (見http://social.msdn.microsoft.com/Forums/en-AU/sharepointdevelopment/thread/e4e00092-b312-4d4c-a0d2-1cfc2beb9a6c
  • 的「HTTP PUT」的方法(即WebDAV的...)只能將文檔庫中,而不是設置字段值
  • 更新字段值,你可以調用後Lists.UpdateListItem了「 HTTP PUT」
  • 文檔庫可以有目錄,你可以讓他們以‘http MKCOL’
  • 你可能要檢查的文件與Lists.CheckInFile
  • 你也可以創建一個使用SPxxx定製的web服務。 Net API,但是新的webservice必須安裝在服務器上。它可以節省旅行到服務器。
6
public static void UploadFile(byte[] fileData) { 
    var copy = new Copy { 
    Url = "http://servername/sitename/_vti_bin/copy.asmx", 
    UseDefaultCredentials = true 
    }; 

    string destinationUrl = "http://servername/sitename/doclibrary/filename"; 
    string[] destinationUrls = {destinationUrl}; 

    var info1 = new FieldInformation 
       { 
        DisplayName = "Title", 
        InternalName = "Title", 
        Type = FieldType.Text, 
        Value = "New Title" 
       }; 

    FieldInformation[] info = {info1}; 
    var copyResult = new CopyResult(); 
    CopyResult[] copyResults = {copyResult}; 

    copy.CopyIntoItems(
    destinationUrl, destinationUrls, info, fileData, out copyResults); 
} 

注:更改的CopyIntoItems的文件名,Path.GetFileName(destinationUrl)的第一個參數,使得取消鏈接消息消失。

相關問題