通过内置的Web服务将文件上传到SharePoint
时间:2020-03-05 18:44:16 来源:igfitidea点击:
通过WSS 3.0版本公开的内置Web服务将文件上传到SharePoint服务器上的文档库的最佳方法是什么?
遵循两个最初的答案...
- 我们肯定需要使用Web服务层,因为我们将从远程客户端应用程序进行这些调用。
- WebDAV方法适用于我们,但我们希望与Web服务集成方法保持一致。
There is additionally a web service to upload files, painful but works all the time.
我们是指复制服务吗?
我们已经成功使用了此服务的CopyIntoItems方法。这是仅使用WSS Web服务API将文件上传到文档库的推荐方法吗?
我已经发布了我们的代码作为建议答案。
解决方案
回答
从工作中的大学:
Lazy way: your Windows WebDAV filesystem interface. It is bad as a programmatic solution because it relies on the WindowsClient service running on your OS, and also only works on websites running on port 80. Map a drive to the document library and get with the file copying. There is additionally a web service to upload files, painful but works all the time. I believe you are able to upload files via the FrontPage API but I don’t know of anyone who actually uses it.
回答
不确定确切使用哪个Web服务,但是如果我们可以使用SharePoint .NET API Dll,那么使用SPList和SPLibrary.Items.Add确实很容易。
回答
使用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) { // ... } }
回答
我使用下面介绍的DocLibHelper包装器类祝我们好运:http://geek.hubkey.com/2007/10/upload-file-to-sharepoint-document.html
回答
另一种选择是使用普通的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)",使取消链接消息消失。