1

I want find and move all documents from one folder to other in different site collection document library. Both the folders are at the sub folder level 3 from the actual list. Please let me know how can I achieve this from client object model.

Folder structure are same in both libraries except the name of root folder

Library A

->Sub Folder 1 ->Sub Folder 2 ->Source Folder (Move the documents from this folder)static folder

Library B

->Sub Folder 1 ->Sub Folder 2 ->Destination Folder (Move the documents to this folder)name different

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

    // set up the destination context
    ClientContext destContext = new ClientContext(destUrl);

    // get the source list and items
    Web srcWeb = srcContext.Web;
    List srcList = srcWeb.Lists.GetByTitle("Components");

    ListItemCollection itemColl = srcList.GetItems(new CamlQuery());
    srcContext.Load(itemColl);
    srcContext.ExecuteQuery(); 
    ListItemCreationInformation info = new ListItemCreationInformation();
    info.FolderUrl = string.Format("{0}/list/{1}/{2}", srcUrl, srcLibrary, "/articlepages/Kishantest/");
    var listitem = srcList.AddItem(info);
    listitem.Update();
    srcContext.ExecuteQuery();

    srcContext.Load(itemColl);
    srcContext.ExecuteQuery();

  // get the destination list
   Web destWeb = destContext.Web;
   destContext.Load(destWeb);
   destContext.ExecuteQuery();
   foreach (var doc in itemColl)
       {
           try
           {

     File file = doc.File;
     srcContext.Load(file);
     srcContext.ExecuteQuery();

     // build destination url
     string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + file.Name;
    // read the file, copy the content to new file at new location
    FileInformation fileInfo = File.OpenBinaryDirect(srcContext, file.ServerRelativeUrl);
    File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
    srcContext.Load(file);
    srcContext.ExecuteQuery();
    }

       catch (Exception ex)
       {

       }
    }
       } 

Calling Function

CopyDocuments("sitecolletionurl" + ddlsourcelocation.SelectedValue.ToString(), "/Components/articlepages/Kishantest", "sitecolletionurl" + ddllangauge.SelectedValue.ToString(), "Components/articlepages" + "/" + txtarticlename.Text.ToString());
1

//Read Source file into byte array

FileInformation srcfile =  Microsoft.SharePoint.Client.File.OpenBinaryDirect(src_clientContext,file.ServerRelativeUrl);
byte[] bytesarr = ReadStream(srcfile.Stream);

public byte[] ReadStream(Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (MemoryStream ms = new MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

// destination library creates file from byte array of source file

var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = bytesarr;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = "URL to Upload";

Microsoft.SharePoint.Client.File uploadFile = List.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
dest_clientContext.ExecuteQuery();

// After file added to destination library you can delete it from source library if want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.