Click here to Skip to main content
15,886,622 members
Articles / Productivity Apps and Services / Sharepoint / SharePoint 2010
Tip/Trick

Copy document with versions from SharePoint 2010 to SharePoint 2013 Online site using .Net Managed Client Object Model

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
4 Aug 2015CPOL1 min read 12.6K   243  
Utilizing SharePoint Client Object model to copy files with versions from SharePoint 2010 site to SharePoint 2013 online site.

This tip explains how Client Object Model can be utilized for copying documents with versions from SharePoint 2010 to SharePoint 2013 Online document library.

Assemblies

You need to refer following assemblies with version 15.0.0.0:

Microsoft.SharePoint.Client

Microsoft.SharePoint.Client.Runtime

Using the Code

There are few things to consider before uploading files with versions to a document library programmatically.

  1. Ensure that versioning is enabled in the library settings.
  2. Checked-out files cannot be moved using this method. So, check-in all the files before copying.
  3. If we update the properties of file versions (like- created and created by fields) then a new version will be created with the updated details. Same applies to the latest version. The functionality to save document metadata without creating a version is provided in SharePoint Server Object Model and not in Client Object Model. Refer https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.systemupdate.aspx .

For explanation, I have divided the code in following steps:

Fetch the target folder, where you want to copy the files.
Folder targetSiteFolder = tContext.Web.GetFolderByServerRelativeUrl(targetFolderUrl);

                tContext.Load(targetSiteFolder);

                try
                {
                    tContext.ExecuteQuery();
                }
                catch (Exception ex)
                {
                    //handle exception
                }
Fetch the sourceFolder from where documents need to be copied.
Folder sourceFolder = sContext.Web.GetFolderByServerRelativeUrl(sourceFolderUrl);

sContext.Load(sourceFolder.Files, files => files.Include(
                    file => file.ListItemAllFields
                    ));

                try
                {
                    sContext.ExecuteQuery();
                }
                catch (Exception e)
                {
                    //handle exception
                }

If you need to fetch the folders as well, insert the below code before try..catch block.

sContext.Load(sourceFolder.Folders, folders => folders.Include(
                    folder => folder.Name,
                    folder => folder.ServerRelativeUrl,
                    folder => folder.Files.Include(
                        sFile => sFile.ListItemAllFields
                        )
                    ).Where(folder => folder.Name != "Forms"));
Fetch file versions from source folder.
int fileCount = sourceFolder.Files.Count;

for (int i = 0; i < fileCount; ++i)
            {
                Microsoft.SharePoint.Client.File sFile = sourceFolder.Files[i];
FileVersionCollection sourceFileVersions = sFile.Versions;

                sContext.Load(sourceFileVersions, fileVersions => fileVersions.Include(
                    version => version.Url,
                    version => version.VersionLabel,
                    version => version.CheckInComment
                    ));

                try
                {
                    sContext.ExecuteQuery();
                }
                catch (Exception e)
                {
                    //handle exception
                }
}
Upload files to target folder.

A separate method handles the task of uploading files to the target folder. We will be uploading all the versions first and then the major version.

private static void UploadFiles(string sourceFileUrl, Folder targetFolder, string fileName)
        {
            FileCreationInformation targetFileVersionCreationInfo = new FileCreationInformation();
            targetFileVersionCreationInfo.Overwrite = true;
            try
            {
                WebRequest request = HttpWebRequest.Create(sourceFileUrl);
                request.Credentials = sContext.Credentials;
                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        byte[] verBuffer = new byte[32768];
                        using (MemoryStream versionMS = new MemoryStream())
                        {
                            int read;
                            while ((read = stream.Read(verBuffer, 0, verBuffer.Length)) > 0)
                            {
                                versionMS.Write(verBuffer, 0, read);
                            }
                            versionMS.Seek(0, SeekOrigin.Begin);
                            targetFileVersionCreationInfo.ContentStream = versionMS;
                            tContext.RequestTimeout = System.Threading.Timeout.Infinite;
                            targetFileVersionCreationInfo.Url = targetFolder.ServerRelativeUrl + "/" + fileName;
                            Microsoft.SharePoint.Client.File targetVersionFile = targetFolder.Files.Add(targetFileVersionCreationInfo);

                            try
                            {
                                tContext.ExecuteQuery();
                            }
                            catch (Exception ex)
                            {
                                //handle exception
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //handle exception
            }
        }

When uploading versions to the target folder, we need to upload each version file in the same sequence. 

if (sourceFileVersions.AreItemsAvailable)
{
foreach (FileVersion fileVer in sourceFileVersions)
     UploadFiles(sContext.Url + "/" + fileVer.Url, targetFolder, sourceItem["FileLeafRef"].ToString());

}

After uploading versions, we will be uploading the main document (latest version).

UploadFiles(sourceItem["FileRef"].ToString(), targetFolder, sourceItem["FileLeafRef"].ToString());

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Aon
India India
Microsoft certified technology specialist with over 5 years of experience in analyzing, designing and developing various segments of Software Development Life Cycle (SDLC), using Microsoft SharePoint 2010/2013.

Comments and Discussions

 
-- There are no messages in this forum --