none
Version conflict while modifying second version of a document RRS feed

  • Question

  • I'm trying to load document versions and appropriate data by creating a C# application, but am getting version conflict error during updating properties in the second version of a document. This is a minimum code that shows the algorithm I have used to create first and next version.

    I was comparing the data in SharePoint and values of variables MajorVersion and MinorVersion of sharePointFile and they are equal.


            public static void manageSingleDoc(String name, String title1, String fn1, String title2, String fn2)
            {
                SP.ClientContext context = new SP.ClientContext(SHAREPOINT_DESTINATION);
                SP.Web web = context.Web;
                string url = "/" + LIST_NAME + "/";
                string fnUrl = url + name;
                System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;
                Microsoft.SharePoint.Client.File sharePointFile;

                if (context.HasPendingRequest) context.ExecuteQuery();
                // create document 'name' in Sharepoint using content of file fn1
                using( Stream fs = File.OpenRead(fn1)) {
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
                    fs.Close();
                }
                sharePointFile = web.GetFileByServerRelativeUrl(fnUrl);
                context.Load(sharePointFile);
                context.ExecuteQuery();
                sharePointFile.CheckOut();
                context.ExecuteQuery();
                SP.List docList = web.Lists.GetByTitle(LIST_NAME);

                // find internal name of property to modify
                context.Load(docList);
                context.ExecuteQuery();
                context.Load(docList.Fields.GetByTitle(TITLE_ATT_NAME));
                context.ExecuteQuery();
                var title = docList.Fields.GetByTitle(TITLE_ATT_NAME).InternalName;

                // modify property of created document
                sharePointFile.ListItemAllFields[title] = title1;
                sharePointFile.ListItemAllFields.Update();
                context.Load(sharePointFile);
                context.ExecuteQuery();
                sharePointFile.CheckIn("First Major version", SP.CheckinType.OverwriteCheckIn);
                sharePointFile.Context.ExecuteQuery();

                // Second major version
                sharePointFile = web.GetFileByServerRelativeUrl(fnUrl);
                context.Load(sharePointFile);
                context.ExecuteQuery();
                // checkout to create next major version
                sharePointFile.CheckOut();
                context.Load(sharePointFile);
                context.ExecuteQuery();
                sharePointFile.ListItemAllFields[title] = title2;
                sharePointFile.ListItemAllFields.Update();
                context.Load(sharePointFile);
                context.ExecuteQuery();                         // <---------                           here the exception is thrown

                using (Stream fs = File.OpenRead(fn2))
                {
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
                    fs.Close();
                }
                sharePointFile.CheckIn("Second major version", SP.CheckinType.MajorCheckIn);
                sharePointFile.Context.ExecuteQuery();


            }

            static void Main(string[] args)
            {

                manageSingleDoc("Test.doc", "value in version 1.0", FN1, "value in version 2.0", FN2);

    }

       


    • Edited by Furli Thursday, November 16, 2017 12:59 PM
    Thursday, November 16, 2017 12:53 PM

Answers

  • Hi,

    Try this:

    One thread for your reference.

    https://sharepoint.stackexchange.com/questions/73187/javascript-how-to-handle-version-conflict-errors-when-using-javascript-com

    class Program
        {
            const string SHAREPOINT_DESTINATION = "http://sp";
            const string LIST_NAME = "MyDoc";
            const string TITLE_ATT_NAME = "Title";
            public static void manageSingleDoc(String name, String title1, String fn1, String title2, String fn2)
            {
                ClientContext context = new ClientContext(SHAREPOINT_DESTINATION);
                Web web = context.Web;
                string url = "/" + LIST_NAME + "/";
                string fnUrl = url + name;
                System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;
                Microsoft.SharePoint.Client.File sharePointFile;
    
                if (context.HasPendingRequest) context.ExecuteQuery();
                // create document 'name' in Sharepoint using content of file fn1
                using (Stream fs = System.IO.File.OpenRead(fn1))
                {
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
                    fs.Close();
                }
                sharePointFile = web.GetFileByServerRelativeUrl(fnUrl);
                context.Load(sharePointFile);
                context.ExecuteQuery();            
                List docList = web.Lists.GetByTitle(LIST_NAME);
    
                // find internal name of property to modify
                context.Load(docList);
                context.ExecuteQuery();
                context.Load(docList.Fields.GetByTitle(TITLE_ATT_NAME));
                context.ExecuteQuery();
                
                sharePointFile.CheckOut();
                //context.ExecuteQuery();
                var title = docList.Fields.GetByTitle(TITLE_ATT_NAME).InternalName;
    
                // modify property of created document
                sharePointFile.ListItemAllFields[title] = title1;
                sharePointFile.ListItemAllFields.Update();
                //context.Load(sharePointFile);
                //context.ExecuteQuery();
                sharePointFile.CheckIn("First Major version", CheckinType.OverwriteCheckIn);
                context.ExecuteQuery();
    
                // Second major version
                //var sharePointFile_2nd = web.GetFileByServerRelativeUrl(fnUrl);
                var item = sharePointFile.ListItemAllFields;
                context.Load(item);
                context.Load(item.File);
                //context.Load(sharePointFile_2nd);
                context.ExecuteQuery();
                var File_2nd = item.File;
                // checkout to create next major version            
                //context.Load(sharePointFile);
                //sharePointFile_2nd.ExecuteQuery();
               
                //context.Load(sharePointFile);            
                //context.ExecuteQuery();                         // <---------                           here the exception is thrown 
    
                using (Stream fs = System.IO.File.OpenRead(fn2))
                {
                    if (context.HasPendingRequest)
                        context.ExecuteQuery();
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
                    fs.Close();
                }
                File_2nd.CheckOut();
                File_2nd.ListItemAllFields[title] = title2;
                File_2nd.ListItemAllFields.Update();
    
                File_2nd.CheckIn("Second major version", CheckinType.MajorCheckIn);
                context.ExecuteQuery();
    
    
            }
            static void Main(string[] args)
            {
                manageSingleDoc("Test.doc", "value in version 1.0", @"C:\Lee\Addin.docx", "value in version 2.0", @"C:\Lee\Addin1.docx");
                Console.WriteLine("Done");
                Console.ReadKey();
    
            }
    
        }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Furli Monday, November 20, 2017 1:46 PM
    Friday, November 17, 2017 6:02 AM

All replies

  • Hi Furli,

    Please try to add below line before sharePointFile.ListItemAllFields.Update(); in the second major version change code

    sharePointFile.ListItemAllFields[owshiddenversion] = sharePointFile.ListItemAllFields[owshiddenversion] + 1;

    This is to increase hidden version number to avoid version conflict error. I hope it helps.

    Thanks & Regards,

    Ramesh

    Friday, November 17, 2017 3:56 AM
  • Hi,

    Try this:

    One thread for your reference.

    https://sharepoint.stackexchange.com/questions/73187/javascript-how-to-handle-version-conflict-errors-when-using-javascript-com

    class Program
        {
            const string SHAREPOINT_DESTINATION = "http://sp";
            const string LIST_NAME = "MyDoc";
            const string TITLE_ATT_NAME = "Title";
            public static void manageSingleDoc(String name, String title1, String fn1, String title2, String fn2)
            {
                ClientContext context = new ClientContext(SHAREPOINT_DESTINATION);
                Web web = context.Web;
                string url = "/" + LIST_NAME + "/";
                string fnUrl = url + name;
                System.Net.ICredentials creds = System.Net.CredentialCache.DefaultCredentials;
                Microsoft.SharePoint.Client.File sharePointFile;
    
                if (context.HasPendingRequest) context.ExecuteQuery();
                // create document 'name' in Sharepoint using content of file fn1
                using (Stream fs = System.IO.File.OpenRead(fn1))
                {
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
                    fs.Close();
                }
                sharePointFile = web.GetFileByServerRelativeUrl(fnUrl);
                context.Load(sharePointFile);
                context.ExecuteQuery();            
                List docList = web.Lists.GetByTitle(LIST_NAME);
    
                // find internal name of property to modify
                context.Load(docList);
                context.ExecuteQuery();
                context.Load(docList.Fields.GetByTitle(TITLE_ATT_NAME));
                context.ExecuteQuery();
                
                sharePointFile.CheckOut();
                //context.ExecuteQuery();
                var title = docList.Fields.GetByTitle(TITLE_ATT_NAME).InternalName;
    
                // modify property of created document
                sharePointFile.ListItemAllFields[title] = title1;
                sharePointFile.ListItemAllFields.Update();
                //context.Load(sharePointFile);
                //context.ExecuteQuery();
                sharePointFile.CheckIn("First Major version", CheckinType.OverwriteCheckIn);
                context.ExecuteQuery();
    
                // Second major version
                //var sharePointFile_2nd = web.GetFileByServerRelativeUrl(fnUrl);
                var item = sharePointFile.ListItemAllFields;
                context.Load(item);
                context.Load(item.File);
                //context.Load(sharePointFile_2nd);
                context.ExecuteQuery();
                var File_2nd = item.File;
                // checkout to create next major version            
                //context.Load(sharePointFile);
                //sharePointFile_2nd.ExecuteQuery();
               
                //context.Load(sharePointFile);            
                //context.ExecuteQuery();                         // <---------                           here the exception is thrown 
    
                using (Stream fs = System.IO.File.OpenRead(fn2))
                {
                    if (context.HasPendingRequest)
                        context.ExecuteQuery();
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(context, fnUrl, fs, true);
                    fs.Close();
                }
                File_2nd.CheckOut();
                File_2nd.ListItemAllFields[title] = title2;
                File_2nd.ListItemAllFields.Update();
    
                File_2nd.CheckIn("Second major version", CheckinType.MajorCheckIn);
                context.ExecuteQuery();
    
    
            }
            static void Main(string[] args)
            {
                manageSingleDoc("Test.doc", "value in version 1.0", @"C:\Lee\Addin.docx", "value in version 2.0", @"C:\Lee\Addin1.docx");
                Console.WriteLine("Done");
                Console.ReadKey();
    
            }
    
        }

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Furli Monday, November 20, 2017 1:46 PM
    Friday, November 17, 2017 6:02 AM
  • Hi Lee!

    Thank you for your help. Though I did not use your code directly (I have some special requirements, like content of a file between versions might not change etc.), you gave me a starting point to work on. Since I'm far from being proficient, please the code is probably not the most efficient and elegant, but for me, the following works:

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using Microsoft.SharePoint.Client;
    
    namespace TransferToSharepoint
    {
        class Program
        {
            static string SHAREPOINT_DESTINATION = "YourSharePointURL";
            static string LIST_NAME = "YourLibraryName";
            static string TITLE_ATT_NAME = "Title";
            static string FILE_NAME = "TitleOfADocumentInSharePoint";
            static string FIRST_VERSION_OF_DOCUMENT = @"PathToAFileToTransfer";
    
            class Transfer
            {
                public ClientContext context;
                public System.Net.ICredentials creds;
                List docList;
    
                public Transfer(String spURL, String list)
                {
                    creds = System.Net.CredentialCache.DefaultCredentials;
                    context = new ClientContext(spURL);
                    docList = context.Web.Lists.GetByTitle(list);
                    context.Load(docList);
                    context.ExecuteQuery();
                    context.Load(docList);
                    context.Load(docList.Fields.GetByTitle(TITLE_ATT_NAME));
                    context.ExecuteQuery();
                }
    
                public string updateMetadata(List docList, Microsoft.SharePoint.Client.File newFile, String fn)
                {
                    //*********NEED TO GET THE INTERNAL COLUMN NAMES FROM SHAREPOINT************
                    var title = docList.Fields.GetByTitle(TITLE_ATT_NAME).InternalName;
                    String documentUrlVar = newFile.ServerRelativeUrl;
                    newFile.ListItemAllFields[title] = fn;
                    newFile.ListItemAllFields.Update();
                    return documentUrlVar;
                }
    
                public void SaveToSP(ClientContext ctx, String from, String to)
                {
                    if (ctx.HasPendingRequest)
                        ctx.ExecuteQuery();
                    Stream fs = System.IO.File.OpenRead(from);
                    Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, to, fs, true);
                    fs.Close();
                }
    
                public void CreateNewDocument(String UploadedFilePath, String fn)
                {
                    String urlAssigned = null;
                    string fnUrl = "/" + LIST_NAME + "/" + fn;
    
                    SaveToSP(context, UploadedFilePath, fnUrl);
                    Microsoft.SharePoint.Client.File sharePointFile = context.Web.GetFileByServerRelativeUrl(fnUrl);
                    context.Load(sharePointFile);
                    context.ExecuteQuery();
    
                    sharePointFile.CheckOut();
                    context.ExecuteQuery();
    
                    urlAssigned = updateMetadata(docList, sharePointFile, fn);
                    context.Load(sharePointFile);
                    context.ExecuteQuery();
    
                    SaveToSP(context, UploadedFilePath, fnUrl);
                    sharePointFile.CheckIn("First Version of a document", CheckinType.OverwriteCheckIn);
                    sharePointFile.Context.ExecuteQuery();
    
                }
    
                public void UpdateDocument(String UploadedFilePath, String fn)
                {
                    String urlAssigned;
                    string fnUrl = "/" + LIST_NAME + "/" + fn;
    
                    Microsoft.SharePoint.Client.File sharePointFile = context.Web.GetFileByServerRelativeUrl(fnUrl);
                    context.ExecuteQuery();
    
                    var item = sharePointFile.ListItemAllFields;
                    context.Load(sharePointFile);
                    context.Load(item, set => set.File, set => set.File.ListItemAllFields);
                    context.ExecuteQuery();
    
                    var File_2nd = item.File;
                    FileInfo nf = new FileInfo(UploadedFilePath);
                    bool diffInFile = File_2nd.Length != 0 || nf.Length != 0;
                    File_2nd.CheckOut();
                    SaveToSP(context, UploadedFilePath, fnUrl);
                    context.Load(sharePointFile);
                    context.Load(item, set => set.File, set => set.File.ListItemAllFields);
                    context.ExecuteQuery();
                    urlAssigned = updateMetadata(docList, File_2nd, fn);
                    File_2nd.CheckIn("New version transfered", CheckinType.MajorCheckIn);
                    context.ExecuteQuery();
                }
            }
    
            static void Main(string[] args)
            {
                Transfer tr = new Transfer(SHAREPOINT_DESTINATION, LIST_NAME);
    
                tr.CreateNewDocument(FIRST_VERSION_OF_DOCUMENT, FILE_NAME);
                tr.UpdateDocument(FIRST_VERSION_OF_DOCUMENT, FILE_NAME);
            }
        }
    }
    

    Thanks again

    Furli

    Monday, November 20, 2017 1:46 PM