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

查看:18
本文介绍了将文件复制到 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 事件.SPItemEventProperties 将通过ListItem 属性.

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

此方法将任何列表项及其关联文件和属性复制到同一网站集中的任何位置(也可能是其他 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);
    }
}

方法二:流式复制,手动设置属性

仅当您需要更多地控制复制哪些项目属性或需要更改文件内容时,才需要此方法.

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天全站免登陆