I am trying to copy documents from one site to the other that's on different site collection. Following code works good but meta data is not being copied. How can I accomplish this?
I have custom fields like "Order Date", "Order Type" and I want to copy meta data of these from source site to the destination
public static void CopyDocuments(string srcUrl, string destUrl, string[] srcLibraryList)
{
// set up the src client
ClientContext srcContext = new ClientContext(srcUrl);
Web srcWeb = srcContext.Web;
// set up the destination context
ClientContext destContext = new ClientContext(destUrl);
// get the list and items
foreach (string library in srcLibraryList)
{
string srcLibrary = library.Trim().TrimStart();
string destLibrary = srcLibrary;
string reportingPeriod = string.Empty;
string reportingPeriodValue = string.Empty;
List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
ListItemCollection col = srcList.GetItems(new CamlQuery());
srcContext.Load(col);
srcContext.ExecuteQuery();
// get the new list
Web destWeb = destContext.Web;
destContext.Load(destWeb);
destContext.ExecuteQuery();
foreach (var doc in col)
{
try
{
if (doc.FileSystemObjectType == FileSystemObjectType.File)
{
// get the file
File f = doc.File;
srcContext.Load(f);
srcContext.ExecuteQuery();
// build new location url
string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary
+ "/" + f.Name;
// read the file, copy the content to new file at new location
FileInformation fileInfo = File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
}
}
catch (Exception ex)
{
//Log("File Error = " + ex.ToString());
}
}
}
}