【问题标题】:Sharepoint CSOM Copy file to other site collectionSharepoint CSOM 将文件复制到其他网站集
【发布时间】:2018-05-23 00:50:14
【问题描述】:

我想将文档库中的文档及其所有附加元数据从一个库复制到另一个网站集中的另一个库。

我想在 C# 中的远程事件接收器中执行此操作。

我的问题主要是:我该如何开始?如果它在同一个网站集中,我可以将文档复制到同一个上下文中的新库,但我想我现在需要在不同的上下文中工作?

这是我现在的代码:

//Get current Item
                List curList = clientContext.Web.Lists.GetById(properties.ItemEventProperties.ListId);
                ListItem curItem = curList.GetItemById(properties.ItemEventProperties.ListItemId);

                clientContext.Load(curItem);
                clientContext.ExecuteQuery();

                string sNewSite = "url.toOtherSitecollection.com";

                //Can we attempt contextception?
                using (ClientContext siteCollContext = new ClientContext(sNewSite))
                {

                    List destinationList = siteCollContext.Web.Lists.GetByTitle("DocumentLibrary_0001");
                    Folder destinationFolder = siteCollContext.Web.GetFolderByServerRelativeUrl("DocumentLibrary_0001");

                    FileCreationInformation newFileCreation = new FileCreationInformation { Content = Convert.FromBase64String(curItem.ToString()), Overwrite = true };

                    File newFile = destinationFolder.Files.Add(newFileCreation);

                    ListItem newItem = newFile.ListItemAllFields;



                    //Copy all the metadata as well.
                    try
                    {
                        newItem["..."] = curItem["..."];
                        //And all other metadata fields...

                    }
                    catch
                    {
                        //Log this.
                    }

                    newItem.Update();
                    siteCollContext.ExecuteQuery();
                }

【问题讨论】:

  • 您必须为其他站点创建单独的上下文并上传文件

标签: c# sharepoint csom


【解决方案1】:

有类似的情况,这里是我的解决方案供参考:

  1. 使用身份验证为源站点和目标站点创建 ClientContext
  2. 调用源站打开我们要复制的文件的文件信息
  3. 将流从 FileInformation 读取到 MemoryStream 中,将用作新复制文件的文件创建信息的 Content 参数
  4. 将新的 FileCrationInformation 添加到目标文件中

    static private void CopyFile()
    {
    
        ClientContext contextOrigin = new ClientContext(originSiteURL);
        ClientContext contextDestination = new ClientContext(destinationSiteURL);
    
        contextOrigin.Credentials = new SharePointOnlineCredentials(originUser, originSecurePassword);
        contextDestination.Credentials = new SharePointOnlineCredentials(destinationUser, destinationSecurePassword);
    
        //Grab File
        FileInformation fileInformation = Microsoft.SharePoint.Client.File.OpenBinaryDirect(contextOrigin, "/path/to/Document.docx"); //Server relative url
        contextOrigin.ExecuteQuery();
    
        //Read Stream
        using (MemoryStream memoryStream = new MemoryStream())
        {
            CopyStream(fileInformation.Stream, memoryStream);
            memoryStream.Seek(0, SeekOrigin.Begin);
    
            //Create Copy
            var fileCreationInfo = new FileCreationInformation
            {
                ContentStream = memoryStream,
                Overwrite = true,
                Url = Path.Combine("path/to/", "CopiedDocument.docx")
            };
    
            var uploadFile = contextDestination.Web.RootFolder.Files.Add(fileCreationInfo);
            contextDestination.Load(uploadFile);
            contextDestination.ExecuteQuery();
    
        }
    }
    

发现复制流方法将现有流的内存复制到新流中。看来FileCreationInfo流不能直接从FileInformation流中分配而不复制到本地。

    static private void CopyStream(Stream source, Stream destination)
    {
        byte[] buffer = new byte[32768];
        int bytesRead;
        do
        {
            bytesRead = source.Read(buffer, 0, buffer.Length);
            destination.Write(buffer, 0, bytesRead);
        } while (bytesRead != 0);
    }

使用 FileInformation 也会复制元数据。

【讨论】:

  • 提供一些关于您的代码如何工作的解释会很有帮助。
猜你喜欢
  • 2012-08-15
  • 2015-06-22
  • 2019-01-06
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2023-01-13
  • 1970-01-01
相关资源
最近更新 更多