How to copy files with metadata from one site to the other programmatically using client object model

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

September 8th, 2015 3:10pm

Have a look on below given earlier discussed threads having suggested solutions that might helps you to get in more details lets you to get this job done:

How to copy file(not folder) from one document library to another library by using client object model in sharepoint 2010

Copy document from one site to another

Also take a look on this article which lets you how to use the content deployment and migration API which has been implemented exactly for this purpose.

Moreover, If you wish you may also try this solution which lets you to migrate from SharePoint 2010 to SharePoint 2013 without any interruption.

Hope it helps you!

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 1:51am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics