Restore File Version in SharePoint using CSOM

The example in this topic show how to use CSOM to Restore File Version in SharePoint

  • using Microsoft.SharePoint.Client;
  • using System.Linq;
  •  
  • using (ClientContext clientContext = new ClientContext("http://MyServer/sites/MySiteCollection"))
  • {
  • // clientcontext.Web.Lists.GetById - This option also can be used to get the list using List GUID
  • // This value is NOT List internal name
  • List targetList = clientcontext.Web.Lists.GetByTitle("Documents");
  •  
  • // Option 1: Get Item by ID
  • ListItem oItem = targetList.GetItemById(13);
  •  
  • // Option 2: Get Item using CAML Query
  • CamlQuery oQuery = new CamlQuery();
  •  
  • oQuery.ViewXml = @"<View><Query><Where>
  • <Eq>
  • <FieldRef Name='LinkFilename' />
  • <Value Type='Text'>document.docx</Value>
  • </Eq>
  • </Where></Query></View>";
  •  
  • ListItemCollection oItems = targetList.GetItems(oQuery);
  • clientcontext.Load(oItems);
  • clientcontext.ExecuteQuery();
  •  
  • oItem = oItems.FirstOrDefault();
  • // Option 2: Ends Here(Above line)
  •  
  • // NOTE: Version Label is nothing but the version number you see in the Version History
  • oItem.File.Versions.RestoreByLabel("2.2");
  •  
  • clientcontext.ExecuteQuery();
  • }

Thank you for reading this article. This code was tested in SharePoint 2013

We value your feedback:

Page URL:

Name:

 

Email:

 
 

Feedback:

 

© 2019 Code SharePoint