Sharepoint 2010 API使用元数据上载文件,从ASP.NET Web应用程序创建新版本

时间:2022-11-09 03:35:29

How I can upload a file with metadata creating a new version from ASP.NET web application to SharePoint 2010?

如何上传包含元数据的文件,从ASP.NET Web应用程序创建新版本到SharePoint 2010?

Is there any way?

有什么办法吗?

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

There are multiple ways to upload a file to a document library.

有多种方法可以将文件上载到文档库。

Perhaps the simplest way is simply to copy the file to the document library's URL using File.Copy. SharePoint supports WebDAV so you can treat a library as a network folder, opening/saving files directly from any application, copying and renaming from Windows Explorer etc.

也许最简单的方法就是使用File.Copy将文件复制到文档库的URL。 SharePoint支持WebDAV,因此您可以将库视为网络文件夹,直接从任何应用程序打开/保存文件,从Windows资源管理器中复制和重命名等。

You do face some limitations though:

你确实面临一些限制:

  • The full path of the file can't be larger than 260 characters and SharePoint will truncate it without warning.
  • 该文件的完整路径不能超过260个字符,SharePoint将在不发出警告的情况下截断它。
  • The file name can't contain some characters that are valid for Windows Explorer, eg #
  • 文件名不能包含对Windows资源管理器有效的某些字符,例如#
  • If you try to save a file with the same name as an existing one, SharePoint will create a new file with a slightly different name without asking
  • 如果您尝试保存与现有文件同名的文件,SharePoint将创建一个名称略有不同的新文件,而不会询问
  • You have no control over the author and creation date properties. SharePoint will use the current date and the credentials of the user executing the code.
  • 您无法控制作者和创建日期属性。 SharePoint将使用当前日期和执行代码的用户的凭据。
  • You can't set any metadata although you can update the file's ListItem properties after uploading.
  • 尽管您可以在上载后更新文件的ListItem属性,但无法设置任何元数据。

Another option is to use the Client object model and upload the file using File.SaveBinaryDirect. This option is better than simply copying the file, as it allows you to overwrite an existing file, but you still don't get direct access to the file's properties. Uploading could be as simple as this:

另一种选择是使用Client对象模型并使用File.SaveBinaryDirect上传文件。此选项优于简单复制文件,因为它允许您覆盖现有文件,但仍然无法直接访问文件的属性。上传可以像这样简单:

var clientContext = new ClientContext("http://intranet.contoso.com");
    using (var fileStream =new FileStream("NewDocument.docx", FileMode.Open))
        ClientOM.File.SaveBinaryDirect(clientContext,
            "/Shared Documents/NewDocument.docx", fileStream, true);

Another option is to connect to a library using the Client Object Model and then create a file using FileCollection.Add, eg

另一种选择是使用客户端对象模型连接到库,然后使用FileCollection.Add创建文件,例如

Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);

Check this SO question, Upload a document to a SharePoint list from Client Side Object Model on how to use both SaveBinaryDirect and FileCollection.Add. The answers display both how to upload a file and how to modify its properties

检查此问题,将文档从客户端对象模型上载到SharePoint列表,了解如何使用SaveBinaryDirect和FileCollection.Add。答案显示如何上载文件以及如何修改其属性

#1


1  

There are multiple ways to upload a file to a document library.

有多种方法可以将文件上载到文档库。

Perhaps the simplest way is simply to copy the file to the document library's URL using File.Copy. SharePoint supports WebDAV so you can treat a library as a network folder, opening/saving files directly from any application, copying and renaming from Windows Explorer etc.

也许最简单的方法就是使用File.Copy将文件复制到文档库的URL。 SharePoint支持WebDAV,因此您可以将库视为网络文件夹,直接从任何应用程序打开/保存文件,从Windows资源管理器中复制和重命名等。

You do face some limitations though:

你确实面临一些限制:

  • The full path of the file can't be larger than 260 characters and SharePoint will truncate it without warning.
  • 该文件的完整路径不能超过260个字符,SharePoint将在不发出警告的情况下截断它。
  • The file name can't contain some characters that are valid for Windows Explorer, eg #
  • 文件名不能包含对Windows资源管理器有效的某些字符,例如#
  • If you try to save a file with the same name as an existing one, SharePoint will create a new file with a slightly different name without asking
  • 如果您尝试保存与现有文件同名的文件,SharePoint将创建一个名称略有不同的新文件,而不会询问
  • You have no control over the author and creation date properties. SharePoint will use the current date and the credentials of the user executing the code.
  • 您无法控制作者和创建日期属性。 SharePoint将使用当前日期和执行代码的用户的凭据。
  • You can't set any metadata although you can update the file's ListItem properties after uploading.
  • 尽管您可以在上载后更新文件的ListItem属性,但无法设置任何元数据。

Another option is to use the Client object model and upload the file using File.SaveBinaryDirect. This option is better than simply copying the file, as it allows you to overwrite an existing file, but you still don't get direct access to the file's properties. Uploading could be as simple as this:

另一种选择是使用Client对象模型并使用File.SaveBinaryDirect上传文件。此选项优于简单复制文件,因为它允许您覆盖现有文件,但仍然无法直接访问文件的属性。上传可以像这样简单:

var clientContext = new ClientContext("http://intranet.contoso.com");
    using (var fileStream =new FileStream("NewDocument.docx", FileMode.Open))
        ClientOM.File.SaveBinaryDirect(clientContext,
            "/Shared Documents/NewDocument.docx", fileStream, true);

Another option is to connect to a library using the Client Object Model and then create a file using FileCollection.Add, eg

另一种选择是使用客户端对象模型连接到库,然后使用FileCollection.Add创建文件,例如

Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
fileCreationInformation);

Check this SO question, Upload a document to a SharePoint list from Client Side Object Model on how to use both SaveBinaryDirect and FileCollection.Add. The answers display both how to upload a file and how to modify its properties

检查此问题,将文档从客户端对象模型上载到SharePoint列表,了解如何使用SaveBinaryDirect和FileCollection.Add。答案显示如何上载文件以及如何修改其属性