加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 将文件复制到SharePoint中的文档库[已关闭]

发布时间:2020-12-15 06:43:32 所属栏目:百科 来源:网络整理
导读:我在SharePoint中有一个文档库.当一个新文件上传到该库时,我希望它能够自动复制到另一个文档库.我该怎么做? 解决方法 使用项目事件接收器并覆盖 ItemAdded事件. SPItemEventProperties将通过ListItem属性给出对列表项的引用. 有两种方法可以做到这一点(感谢
我在SharePoint中有一个文档库.当一个新文件上传到该库时,我希望它能够自动复制到另一个文档库.我该怎么做?

解决方法

使用项目事件接收器并覆盖 ItemAdded事件. SPItemEventProperties将通过ListItem属性给出对列表项的引用.

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

方法1:使用CopyTo

此方法将具有关联文件和属性的任何列表项复制到同一网站集中的任何位置(可能还有其他Web应用程序,但我还没有测试).如果您查看项目的属性或使用其下拉菜单,SharePoint也会自动维护到源项目的链接.此链接可以用UnlinkFromCopySource删除.

CopyTo的唯一技巧是目标位置需要一个完整的URL.

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

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

如果您需要更多地控制复制哪些项目属性或者需要更改文件的内容,则此方法将是必需的.

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

部署

您也有各种部署选项.您可以将事件接收器与连接到内容类型或列表的功能相关联,并以编程方式添加它们.详见this article at SharePointDevWiki.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读