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());