通过WSS 3.0版本公开的内置Web服务将文件上载到SharePoint服务器上的文档库的最佳方法是什么?
按照两个初步答案......
我们肯定需要使用Web服务层,因为我们将从远程客户端应用程序进行这些调用.
WebDAV方法对我们有用,但我们更愿意与Web服务集成方法保持一致.
还有一个上传文件的网络服务,痛苦但一直有效.
你指的是"复制"服务吗?我们已成功使用此服务的CopyIntoItems
方法.这是仅使用WSS Web服务API将文件上载到文档库的推荐方法吗?
我已将我们的代码发布为建议的答案.
使用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) { // ... } }
另一种选择是使用普通的'HTTP PUT:
WebClient webclient = new WebClient(); webclient.Credentials = new NetworkCredential(_userName, _password, _domain); webclient.UploadFile(remoteFileURL, "PUT", FilePath); webclient.Dispose();
remoteFileURL指向SharePoint文档库的位置...
有几件事需要考虑:
Copy.CopyIntoItems 需要文档已经存在于某个服务器上.该文档作为webservice调用的参数传递,这将限制文档的大小.(见http://social.msdn.microsoft.com/Forums/en-AU/sharepointdevelopment/thread/e4e00092-b312-4d4c-a0d2-1cfc2beb9a6c)
'http put'方法(即webdav ...)只会将文档放入库中,但不会设置字段值
要更新字段值,您可以在'http put'之后调用Lists.UpdateListItem
文档库可以有目录,你可以使用'http mkcol'制作它们
您可能想要使用Lists.CheckInFile签入文件
您还可以创建使用SPxxx .Net API的自定义Web服务,但必须在服务器上安装新的Web服务.它可以节省到服务器的旅行.
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)
,会使取消链接消息消失.