Ejemplo n.º 1
0
        private bool CopyFolder(ClientContext sourceContext, ClientContext destContext, List sourceList, List destList, Folder sourceFolder, Folder destFolder)
        {
            IEnumerable <ListItem> sourceItems = FilterListItemsByServerRelativeUrl(sourceContext, sourceContext.Web, sourceList, new Properties(),
                                                                                    sourceFolder.ServerRelativeUrl, false, "Eq");

            foreach (ListItem sourceListItem in sourceItems)
            {
                ListItem destListItem = null;
                if (sourceList.BaseType != BaseType.DocumentLibrary)
                {
                    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                    itemCreateInfo.FolderUrl = string.Empty;
                    itemCreateInfo.FolderUrl = string.Format("{0}{1}", destContext.Url, destFolder.ServerRelativeUrl);

                    destListItem = destList.AddItem(itemCreateInfo);
                    destContext.ExecuteQuery();
                }
                else
                {
                    sourceContext.Load(sourceListItem.File, f => f.Name);
                    sourceContext.ExecuteQuery();
                    //get current file content
                    byte[] currentFileContent = GetFile(sourceContext, sourceListItem);

                    //add file
                    File newFile = AddFileByFolderPathWithRoot(destContext, destContext.Web, destList, sourceListItem.File.Name,
                                                               currentFileContent, destFolder.ServerRelativeUrl, true);

                    destListItem = newFile.ListItemAllFields;
                    destContext.Load(destListItem);
                    destContext.ExecuteQuery();
                }

                //copy metaData
                SPHelper.CopyItem(sourceContext, destContext, sourceListItem, destListItem);
            }
            FolderCollection childSourceFolders = sourceFolder.Folders;

            sourceContext.Load(childSourceFolders);
            sourceContext.ExecuteQuery();

            foreach (Folder childSourceFolder in childSourceFolders)
            {
                Folder childDestFolder = CreateSubFolderInternalWithoutRecursion(destContext, destList, destFolder, childSourceFolder.Name);
                CopyFolder(sourceContext, destContext, sourceList, destList, childSourceFolder, childDestFolder);
            }

            return(true);
        }