将文件复制到文档中的SharePoint库 [英] Copy files to document library in SharePoint

查看:208
本文介绍了将文件复制到文档中的SharePoint库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SharePoint文档库。当一个新的文件上传到该库我希望它会自动被复制到一个又一个文档库为好。我怎样才能做到这一点。

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?

推荐答案

使用一个项事件接收器和覆盖的ItemAdded~~V 事件。 SPItemEventProperties 会给您通过列表项属性参考列表项

Use an item event receiver and override the ItemAdded event. SPItemEventProperties will give you a reference to the list item via the ListItem property.

有两种方法可以做到这一点(感谢您的CopyTo从发现)。

There are two methods to do this (thanks to your discovery of CopyTo).

方法1:使用 CopyTo从

Method 1: Use CopyTo

这方法复制及其相关文件和属性的任何位置同一站点集合中的任何列表项(可能还有其他Web应用程序,以及,但我没有测试过)。的SharePoint自动维护链接到源项目,以及如果你查看该项目的属性,或使用其下拉菜单。此链接可以 UnlinkFromCopySource 被删除。

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.

的唯一诀窍CopyTo从是一个完整的URL是必需的目标位置。

The only trick to CopyTo is that a full URL is required for the destination location.

public class EventReceiverTest : SPItemEventReceiver
{
    public override void ItemAdded(SPItemEventProperties properties)
    {
    	properties.ListItem.CopyTo(
    		properties.WebUrl + "/Destination/" + properties.ListItem.File.Name);
    }
}



方法2:流副本,手动设置性能

如果您需要对哪些项目属性被复制,或者如果文件内容需要改变更多的控制这种方法就只能是必要的。

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();
    }
}



部署

您已经部署的各种选项,以及。您可以连接到内容类型或列表功能事件接收器相关联,并以编程方式添加。请参见本文在SharePointDevWiki 了解更多详情。

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 SharePointDevWiki for more details.

这篇关于将文件复制到文档中的SharePoint库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆