0

I want to fetch List Item versions and its data per version. How can I achieve it? Here is pseudo example

context.Load(_listitem, tmp => tmp.DisplayName,
                        tmp => tmp.File, tmp => tmp.File.Versions,
                        tmp => tmp.FieldValuesAsText);

context.ExecuteQuery();

if (_listitem.Versions.Count > 0)
{
 foreach (ListItemVersion version in _listitem.Versions)
  {
    if (version.IsCurrentVersion)
        continue;

    int id = version.VersionId;
    string lbl  =  version.VersionLabel;
// I need to get the data  for every version of the listitem
  }
}
4
  • 1
    You are almost there now start using 'version' object to extracts metadata like "version['Modified']" and so on... Commented Jan 3, 2018 at 6:02
  • 1
    Does it worked? Commented Jan 3, 2018 at 6:12
  • Yes. Its working. Thanks for your help I am iterating fieldvalues of the version foreach (var field in version.FieldValues)
    – MSJ
    Commented Jan 3, 2018 at 6:23
  • 1
    Glad that it helped! Commented Jan 3, 2018 at 6:42

3 Answers 3

0

You are almost there now start using version object to extracts metadata like version['Modified'] and so on...

0

Using the code you have to make following steps:

  1. Traverse through the version.
  2. Once you find the specific version, check out the item
  3. Make the specific version as current version.
  4. Access the content and perform operations
  5. Undo check out the item.

Once you follow the steps in the code, you will be able to access the previous version's data as well as the current version will not be affected.

Hope this helps to you. Regards,

0

Here is a demo for your reference:

static void Main(string[] args)
        {

            string siteCollectionUrl = "https://tenantname.sharepoint.com/sites/sitename";
            string userName = "[email protected]";
            string password = "yourpassword";

            // Namespace: Microsoft.SharePoint.Client  
            ClientContext ctx = new ClientContext(siteCollectionUrl);

            // Namespace: System.Security
            SecureString secureString = new SecureString();
            password.ToList().ForEach(secureString.AppendChar);

            // Namespace: Microsoft.SharePoint.Client  
            ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);

            // Namespace: Microsoft.SharePoint.Client  
            Site site = ctx.Site;

            ctx.Load(site);
            ctx.ExecuteQuery();
            Web web = ctx.Web;

            ctx.Load(web,w => w.ServerRelativeUrl,w => w.Lists);

            List list = web.Lists.GetByTitle("List1");

            ctx.Load(list);
            CamlQuery camlQuery = new CamlQuery();
            ListItemCollection itemColl = list.GetItems(camlQuery);

            ctx.Load(itemColl);

            ctx.ExecuteQuery();


            foreach (ListItem item in itemColl)

            {
                Console.WriteLine(item["Title"].ToString());
                ListItemVersionCollection itemversioncollection = item.Versions;

                ctx.Load(itemversioncollection);

                ctx.ExecuteQuery();

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

                {

                    ListItemVersion version = itemversioncollection[iVersionCount];
                    Console.WriteLine(version.FieldValues["Title"].ToString());

                }



            }
        }

And for version object, you can access like version.FieldValues["fieldname"], you can check the field name in Visual Studio like below:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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