How to get list item version history of a custom list using Client Object Model

Below code describes how to get the list item version history of a custom list using Client Object Model

  1. Add reference “Microsoft.sharepoint.client.dll” and “Microsoft.sharepoint.client.Runtime.dll”.
  2. Write Below Code.

class Program

{

static void Main(string[] args)

{

using (ClientContext ctx = new ClientContext(“SITE_URL”))

{

CamlQuery camlQuery = new CamlQuery();

Web web = ctx.Web;

ctx.Load(web,

                    w => w.ServerRelativeUrl,

                    w => w.Lists);

List list = web.Lists.GetByTitle(“LIST_TITLE”);

ctx.Load(list);

ListItemCollection itemColl = list.GetItems(camlQuery);

ctx.Load(itemColl);

ctx.ExecuteQuery();

foreach (ListItem item in itemColl)

{

File fileVersion = web.GetFileByServerRelativeUrl(web.ServerRelativeUrl +    “/Lists/” + list.Title + “/” + item.Id + “_.000”);

ctx.Load(fileVersion);

FileVersionCollection fileVersionCollection = fileVersion.Versions;

ctx.Load(fileVersionCollection);

ctx.ExecuteQuery();

for (int iVersionCount = 0; iVersionCount < fileVersionCollection.Count; iVersionCount++)

{

FileVersion version = fileVersionCollection[iVersionCount];

fileVersionCollection.DeleteByID(version.ID);

}

ctx.ExecuteQuery();

}

}

}

}

Tags: , , ,

7 thoughts on “How to get list item version history of a custom list using Client Object Model

  1. I blog quite often and I really thank you for your information. Your article has really peaked my interest. I’m going to take a note of your blog and keep checking for new information about once a week. I opted in for your Feed as well.

Leave a Reply to Lazaro Creekbaum Cancel reply

Your email address will not be published. Required fields are marked *