C# 将文件复制到 SharePoint 中的文档库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1059175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Copy files to document library in SharePoint
提问by raklos
I have a document library in SharePoint. When a new file is uploaded to that library I want it to automatically get copied to a another document library as well. How can I do this?
我在 SharePoint 中有一个文档库。当一个新文件上传到该库时,我希望它也自动复制到另一个文档库。我怎样才能做到这一点?
采纳答案by Alex Angas
Use an item event receiver and override the ItemAddedevent. SPItemEventPropertieswill give you a reference to the list item via the ListItem property.
使用项目事件接收器并覆盖ItemAdded事件。SPItemEventProperties将通过 ListItem 属性为您提供对列表项的引用。
There are two methods to do this (thanks to your discovery of CopyTo).
有两种方法可以做到这一点(感谢您发现 CopyTo)。
Method 1: Use CopyTo
方法 1:使用CopyTo
This method copies any list item with its associated file and properties to any location in the same site collection (possibly other web applications as well but I haven't tested). SharePoint automatically maintains the link to the source item as well if you view the item's properties or use its drop-down menu. This link can be removed with UnlinkFromCopySource.
此方法将任何列表项及其关联文件和属性复制到同一网站集中的任何位置(也可能是其他 Web 应用程序,但我尚未测试)。如果您查看项目的属性或使用其下拉菜单,SharePoint 也会自动维护指向源项目的链接。可以使用UnlinkFromCopySource删除此链接。
The only trick to CopyTo is that a full URL is required for the destination location.
CopyTo 的唯一技巧是目标位置需要完整的 URL。
public class EventReceiverTest : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
properties.ListItem.CopyTo(
properties.WebUrl + "/Destination/" + properties.ListItem.File.Name);
}
}
Method 2: Stream copy, manually set properties
方法二:流式复制,手动设置属性
This method would only be necessary if you need more control over which item properties are copied or if the file's contents need to be altered.
仅当您需要更多地控制复制哪些项目属性或需要更改文件内容时,才需要此方法。
public class EventReceiverTest : SPItemEventReceiver
{
public override void ItemAdded(SPItemEventProperties properties)
{
SPFile sourceFile = properties.ListItem.File;
SPFile destFile;
// Copy file from source library to destination
using (Stream stream = sourceFile.OpenBinaryStream())
{
SPDocumentLibrary destLib =
(SPDocumentLibrary) properties.ListItem.Web.Lists["Destination"];
destFile = destLib.RootFolder.Files.Add(sourceFile.Name, stream);
stream.Close();
}
// Update item properties
SPListItem destItem = destFile.Item;
SPListItem sourceItem = sourceFile.Item;
destItem["Title"] = sourceItem["Title"];
//...
//... destItem["FieldX"] = sourceItem["FieldX"];
//...
destItem.UpdateOverwriteVersion();
}
}
Deployment
部署
You have various options for deployment as well. You can associate event receivers with a feature connected to a content type or list, and programmatically add them. See this article at SharePointDevWikifor more details.
您也有各种部署选项。您可以将事件接收器与连接到内容类型或列表的功能相关联,并以编程方式添加它们。有关更多详细信息,请参阅SharePointDevWiki 上的这篇文章。