0

In my project I need to migrate the List/DocumentList data along with its version history. I am able to migrate List/DocumentList using CSOM. But not getting solution to write the version data at destination. How can I upload the version data at destination using CSOM?

Thanks in advance.

0

Try the following CSOM code to migrate files with versions:

int fileCount = sourceFolder.Files.Count();
for (int i = 0; i < fileCount; i++)
 {
    Microsoft.SharePoint.Client.File sourceFile = 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   
    }


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

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   
     }
 }

Read the following blog for more information:

https://blogs.msdn.microsoft.com/rakhiguha/2015/12/06/migrate-sharepoint-document-with-version-to-sharepoint-online-using-csom/

|improve this answer|||||

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.