Как я могу скопировать папку из SharePoint 2016 на рабочем месте в Sharepoint онлайн с помощью CSOM? - PullRequest
0 голосов
/ 02 апреля 2020

Я копирую, но работает только с файлами. Как я могу сделать то же самое, но только для папок? Теперь, если я пытаюсь скопировать папку, я ловлю исключение о неправильном пути в этом месте

using (var fileStream = new FileStream(srcUrl, FileMode.Open, FileAccess.Read)) <--Exception

Основной код:

        // set up the src client
        ClientContext srcContext = new ClientContext(srcUrl);

        // get the list and items
        Web srcWeb = srcContext.Web;
        List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
        ListItemCollection itemColl = srcList.GetItems(new CamlQuery());
        srcContext.Load(itemColl);
        srcContext.ExecuteQuery();

        using (var destContext = new OfficeDevPnP.Core.AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(destUrl, login, password))
        {
            // get the new list
            Web destWeb = destContext.Web;
            destContext.Load(destWeb);
            destContext.ExecuteQuery();

            foreach (var doc in itemColl)
            {
                try
                {
                    if (doc.FileSystemObjectType == FileSystemObjectType.File)
                    {
                        // get the file
                        Microsoft.SharePoint.Client.File file = doc.File;
                        srcContext.Load(file);
                        srcContext.ExecuteQuery();

                        // build new location url
                        string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + file.Name;
                        // read the file, copy the content to new file at new location
                        FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl);
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
                    }
                    if (doc.FileSystemObjectType == FileSystemObjectType.Folder)
                    {
                        // load the folder
                        srcContext.Load(doc);
                        srcContext.ExecuteQuery();
                        // get the folder data, get the file collection in the folder
                        Folder folder = srcWeb.GetFolderByServerRelativeUrl(doc.FieldValues["FileRef"].ToString());
                        FileCollection fileCol = folder.Files;
                        // load everyting so we can access it
                        srcContext.Load(folder);
                        srcContext.Load(fileCol);
                        srcContext.ExecuteQuery();

                        foreach (Microsoft.SharePoint.Client.File f in fileCol)
                        {
                            // load the file
                            srcContext.Load(f);
                            srcContext.ExecuteQuery();
                            string[] parts = null;
                            string id = null;
                            if (srcLibrary == "My Files")
                            {
                                // these are doc sets
                                parts = f.ServerRelativeUrl.Split('/');
                                id = parts[parts.Length - 2];
                            }
                            else
                            {
                                id = folder.Name;
                            }
                            // build new location url
                            string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + id + "/" + f.Name;

                            // read the file, copy the content to new file at new location
                            FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);

                            using (var fileStream = new FileStream(srcUrl, FileMode.Open, FileAccess.Read))
                            {
                                Microsoft.SharePoint.Client.File.SaveBinaryDirect(destContext, srcUrl, fileStream, true);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        };
...