0

I have two site collection on SharePoint online.

I want to create web job to move all files to another site collection along with version.

I have created one console application and written code in managed CSOM to copy all files to another site collection but versions are missing.

Is there any way to achieve this?

Following is my code

       private static void CreateFile(Folder folder, ClientContext sourceContext, ClientContext destContext, Microsoft.SharePoint.Client.File file, Web sourceWeb, Web destWeb)
    {
        string newLocation = string.Empty;
        try
        {
            ClientResult<Stream> data = file.OpenBinaryStream();
            ListItem oldItem = file.ListItemAllFields;
            FieldCollection fields = oldItem.ParentList.Fields;
            sourceContext.Load(file);
            sourceContext.Load(oldItem);
            sourceContext.Load(fields);
            sourceContext.ExecuteQuery();


            newLocation = file.ServerRelativeUrl.Replace(sourceWeb.ServerRelativeUrl, destWeb.ServerRelativeUrl);
            FileCreationInformation fileInfo = new FileCreationInformation();
            fileInfo.ContentStream = data.Value;
            fileInfo.Url = newLocation;
            Microsoft.SharePoint.Client.File newFile = folder.Files.Add(fileInfo);
            ListItem newItem = newFile.ListItemAllFields;
            destContext.Load(newFile);
            destContext.Load(newItem);
            destContext.ExecuteQuery();


            foreach (Field field in fields)
            {
                if (!field.ReadOnlyField && !field.Hidden && field.InternalName != "Attachments" && field.Title != "Content Type")
                {
                    try
                    {
                        newItem[field.InternalName] = oldItem[field.InternalName];
                    }
                    catch (Exception ex)
                    {                            
                    }
                }
            }
            newItem.Update();
            if (newFile.CheckOutType != CheckOutType.None)
                newFile.CheckIn("New File", CheckinType.MajorCheckIn);
            destContext.ExecuteQuery();

        }
        catch (Exception ex)
        {               
        }
    }
0

First Make sure that the versioning has been enabled on both libraries.

If the issue is related to code , Try to use the following code to can Migrate Sharepoint document with version to using CSOM

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

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.