0

I am trying to read all the versions of a list on a site and store them. I would like to do it for the whole row rather than column by column (or item by item). I have seen this here https://sharepoint.stackexchange.com/questions/137360/how-to-get-all-major-versions-of-list-or-library-item-using-c but cannot get my head around it as none of the oList, items, and listItem does not have "Version" attribute. I have enabled versioning for my list in the SharePoint. Below is my code in C#.

using (ClientContext clientContext = new ClientContext("https://domain.sharepoint.com/sites/mysite"))
{
    SecureString passWord = new SecureString();
    foreach (char c in "mypass".ToCharArray()) passWord.AppendChar(c);
    clientContext.Credentials = new SharePointOnlineCredentials("email@domain", passWord);
    Web web = clientContext.Web;

    clientContext.Load(web);
    clientContext.ExecuteQuery();
    CamlQuery query = new CamlQuery();
    query.ViewXml = @"";
    SP.List oList = clientContext.Web.Lists.GetByTitle("listname");

    clientContext.Load(oList);
    clientContext.ExecuteQuery();

    SP.ListItemCollection items = oList.GetItems(query);

    clientContext.Load(items);
    clientContext.ExecuteQuery();
    foreach (ListItem listItem in items)
    {

        foreach (KeyValuePair<string, object> dictitem in listItem.FieldValues)
        {
        //Read this key, value and save them in a datatable
        }
    }

}

buy using v16 of SharePoint SDK and items.Versions, I get the following error:

 Error  CS1061  'ListItemCollection' does not contain a definition
 for 'Versions' and no accessible extension method 'Versions' accepting a
  first argument of type 'ListItemCollection' could be found (are you missing
  a using directive or an assembly reference?)       
Mohsen Sichani
  • 1,002
  • 12
  • 33

2 Answers2

0

You could use the ListItem.Versions Property to get all versions of an item:

https://learn.microsoft.com/en-us/dotnet/api/microsoft.sharepoint.client.listitem.versions

foreach (ListItem listItem in items)
{ 
        ListItemVersionCollection itemversioncollection = listItem.Versions;
        clientContext.Load(itemversioncollection);
        clientContext.ExecuteQuery();
}  

Also, use the latest version of SharePoint SDK if possible.

Mohsen Sichani
  • 1,002
  • 12
  • 33
Michael Han
  • 3,475
  • 1
  • 6
  • 8
0

Commenting here, since this answer popped up when I searched for a solution. There is already a solution in another stackoverflow answer. That answer works also for SP13.

It is based on using:

"https://<server>/<site>/_layouts/versions.aspx?list={litsID}&ID=<itemID>" 

Please see SharePoint 2013 - Get SPListItem versions via REST