将文档移到子站点 [英] Move document to Subsite

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

问题描述

我想知道是否可以将文档从一个库移动到子站点"usine CSOM C#"中的文档库

I would like to know if we could move a document from one library to a document library in a subsite  usine CSOM C#

Src-父网站中的图书管理员

Src - librray in Parent site

目标-子站点中的库

我想保留与文档关联的元数据

I would like to retain the metadata associated with the document

File.MoveTo-这仅适用于同一库中的文件夹吗?

File.MoveTo - does this work only for folders within same library?

任何帮助表示赞赏.

此致

阿伦

推荐答案

此处提供了示例代码供您参考(这是跨网站收集的示例,但您可以根据需要更新逻辑)

File.MoveTo用于同一网站.

static void Main(string[] args)
        {
            using (var context = new ClientContext("http://sp"))
            {
                Console.ForegroundColor = ConsoleColor.Green;
                string password = "pw";
                SecureString sec_pass = new SecureString();
                Array.ForEach(password.ToArray(), sec_pass.AppendChar);
                sec_pass.MakeReadOnly();
                context.Credentials = new NetworkCredential(@"contoso\user", password);

                string sourceList = "Doc1";
                Microsoft.SharePoint.Client.FileCollection srcItems = context.Web.Lists.GetByTitle(sourceList).RootFolder.Files;
                context.Load(srcItems);
                context.ExecuteQuery();

                if (srcItems.Count > 0)
                {
                    using (var targetContext = new ClientContext("http://sp:12001"))
                    {
                        targetContext.Credentials = new NetworkCredential(@"contoso\user", password);
                        string targetList = "Doc1";
                        Microsoft.SharePoint.Client.List trgList = targetContext.Web.Lists.GetByTitle(targetList);
                        Folder trgRootFolder = trgList.RootFolder;
                        targetContext.Load(trgList);
                        targetContext.Load(trgRootFolder);
                        targetContext.ExecuteQuery();
                        foreach (var item in srcItems)
                        {
                            ClientResult<Stream> buffer = item.OpenBinaryStream();
                            context.Load(item);
                            context.ExecuteQuery();                            
                            if (buffer != null)
                            {
                                MemoryStream memStream = new MemoryStream();
                                buffer.Value.CopyTo(memStream);

                                // returns a byte[]
                                byte[] bytes = memStream.ToArray();
                                var fileCreationInformation = new FileCreationInformation();

                                fileCreationInformation.Content = bytes;
                                fileCreationInformation.Overwrite = true;
                                fileCreationInformation.Url = trgRootFolder.ServerRelativeUrl+"/" + item.Name;
                                Microsoft.SharePoint.Client.File dFile = trgList.RootFolder.Files.Add(fileCreationInformation);
                                foreach (var field in item.ListItemAllFields.FieldValues)
                                {
                                    if (!(dFile.ListItemAllFields.FieldValues.ContainsKey(field.Key)))
                                    {
                                        dFile.ListItemAllFields.FieldValues.Add(field.Key, field.Value);
                                    }
                                    else
                                    {
                                        dFile.ListItemAllFields.FieldValues[field.Key] = field.Value;
                                    }
                                }
                                dFile.ListItemAllFields.Update();
                                targetContext.Load(dFile);
                                targetContext.ExecuteQuery(); 
                            }

                        }
                    }

                }

                Console.ReadKey();
            }

        }

最好的问候,


这篇关于将文档移到子站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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