0

I have the following code inside my console application:-

CamlQuery camlQuery2 = new CamlQuery();
camlQuery.ViewXml = "<View><Query><Where><Neq><FieldRef Name='ProjectStage'/>" +
"<Value Type='String'>Closed</Value></Neq></Where></Query><RowLimit>100</RowLimit></View>";
ListItemCollection collListItem2 = oList.GetItems(camlQuery2);
context.Load(collListItem2);
foreach (ListItem oListItem in collListItem2){
foreach (var itemversion in oListItem.Versions)

but i am unable to get the ListItem Versions using oListItem.Versions!! where i got this error:-

Error 1 'Microsoft.SharePoint.Client.ListItem' does not contain a definition for 'Versions' and no extension method 'Versions' accepting a first argument of type 'Microsoft.SharePoint.Client.ListItem' could be found (are you missing a using directive or an assembly reference?)

2

The following steps for your reference.

1.Create a console application using visual studio.

2.Install the package using the NuGet below.

Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.19515.12000

3.Refer to the code example below to get list items versions.

using Microsoft.SharePoint.Client;
using System;
using System.Security;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteURL = @"https://tenant.sharepoint.com/sites/team";
            string listTitle = "customlist";
            var login = "test@tenant.onmicrosoft.com";
            var password = "xxx";

            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }
            SharePointOnlineCredentials onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

            ClientContext context = new ClientContext(siteURL);
            context.Credentials = onlineCredentials;
            var oList = context.Web.Lists.GetByTitle(listTitle);
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View><Query><Where><Neq><FieldRef Name='ProjectStage'/>" +
            "<Value Type='String'>Closed</Value></Neq></Where></Query><RowLimit>100</RowLimit></View>";
            ListItemCollection collListItem= oList.GetItems(camlQuery);
            context.Load(collListItem);
            context.ExecuteQuery();
            foreach (ListItem oListItem in collListItem){
                Console.WriteLine("------------" + oListItem.Id);
                context.Load(oListItem.Versions);
                context.ExecuteQuery();
                foreach (ListItemVersion itemversion in oListItem.Versions)
                { 
                    Console.WriteLine(itemversion.VersionLabel+"|"+itemversion.FieldValues["Title"]);
                }
            }
            Console.ReadLine();
        }
    }
}
| improve this answer | |
2

Please refer this article.

In short, you have to refer the newly released CSOM package 16.1.6906.1200 or higher.

You can find the latest CSOM package for SharePoint Online from the NuGet gallery with an id of ‘Microsoft.SharePointOnline.CSOM‘.

Adding the code sample below.

// Get versions of specific document from library - item and file
 List list = ctx.Web.Lists.GetByTitle("DocumentLibraryTitle");
 ListItem item = list.GetItemById(1);
 ListItemVersionCollection versions = item.Versions;
 Microsoft.SharePoint.Client.File file = item.File;
 FileVersionCollection fileVersions = file.Versions;
 ctx.Load(list); ctx.Load(item); ctx.Load(versions);
 ctx.Load(file); ctx.Load(fileVersions);
 ctx.ExecuteQuery();

 // Loop list item versions and access data of specific fields
 foreach (var version in versions)
 {
   string versionValue = string.Empty;
   if (version.FieldValues["FieldName"] != null)
   {
     versionValue = version.FieldValues["FieldName"].ToString();
   }

   // Do something with the value
   Console.WriteLine(version.VersionLabel + " - " + versionValue);
 }

 // Download all versions of specific file as individual docs
 int index = 0;
 foreach (var version in fileVersions)
 {
   var str = version.OpenBinaryStream();
   ctx.ExecuteQuery();
   // Notice that we assume in this case that it's docx file
   string filename = string.Format("d:\downloaded\doc-{0}.docx", index);
   using (var fs = new FileStream(filename, FileMode.OpenOrCreate))
   {
     str.Value.CopyTo(fs);
   }
   index++;
 }
| improve this answer | |
0

Unfortunately, CSOM doesn't yet support this Versions property, you'll have to get through a REST call, see

Get List Item Version History using Client Side Object Model (C#)

and

SPservices to retrieve version history of items in SharePoint List 2007?

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.