Add versions of one file to another in sharepoint 2013 through CSOM

Thakur Harshdeep (SX/BSV-AD2) 1 Reputation point
2022-04-06T06:32:22.897+00:00

I have a document D1 with some old versions, and I am copying this document with File.CopyTo() function and it will create D2 document in the same library. Now all the old versions of D1 document are deleted.

Is it possible to add the old versions details of D2 document to D1 document through CSOM?

Note: SP version is 2013 (on-premise)

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,135 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 36,821 Reputation points Microsoft Vendor
    2022-04-07T07:10:02.503+00:00

    Hi @Thakur Harshdeep (SX/BSV-AD2) ,
    Per my research, there is no such function to add version details to a document. We can only retrieve versions by csom

    var list = ctx.Web.Lists.GetByTitle("Documents");  
    //Use below two lines if you want to load a specific item by id  
    //var item = list.GetItemById(26);  
    //ctx.Load(item, l=>l.ContentType);  
      
    var query = new CamlQuery()  
    {  
        ViewXml = String.Format("<View><Query><Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>Document Set</Value></Eq></Where></Query></View>")  
    };  
      
    ListItemCollection items = list.GetItems(query);  
    ctx.Load(items);  
    ctx.ExecuteQuery();  
      
    foreach (var item in items)  
    {  
    Console.WriteLine("Item id: "+ item.Id);  
    var folder = item.Folder;  
    ctx.Load(folder);  
    var ds = Microsoft.SharePoint.Client.DocumentSet.DocumentSet.GetDocumentSet(ctx, folder);  
    ctx.Load(ds);  
    var dsVersions = ds.VersionCollection;  
    ctx.Load(dsVersions);  
    ctx.ExecuteQuery();  
    Console.WriteLine("Version count: "+ dsVersions.Count);  
    }  
    

    As a workaround, Could we copy D2 to D1 with version details?


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.