How we can retrieve the metadata information of a current approved version of a list item.

Consider you want to get the metadata information of an item’s current approved version. For getting more details please take a look at the below image, it is a version history of list item. (In order to get this you need to enable the versioning and content approval in the version settings of your list)

image

In the above picture you can see the current approved version of the list item (in this case it is version 3.0), and at the time of approving the item the values of my list’s columns might be different. Also, you can see that, after the approval, the list item has three more versions which are 4.0, 5.0 and 6.0 and the values of the columns for all those versions might be different.

 

Consider you want to get the value of current approved version through SharePoint object model. From UI you can see that by clicking the “View” menu item in the ECB menu of that particular version. If you click on that menu you will be redirecting to an another page (see the image below - /_layouts/DispForm.aspx).

clip_image003

Here you can see that the metadata information of that selected version of the list item. But if you try to access the list item of the current approved version like below, it will just return the latest version’s item metadata information, which is for Title it is – “My FirstItem – Edited – fifth time” , and it won’t be “MyFirstItem – Edited- second time again” - Title column's value of current approved version.

            SPList oList = oWeb.Lists["Test1"]; 

            SPListItem oListItem = oList.Items.GetItemById(1);

            SPListItemVersionCollection oVersionInfo = oListItem.Versions;

            foreach (SPListItemVersion oVersion in oVersionInfo)

            {

                if (oVersion.Level == SPFileLevel.Published)

                {

                    Console.WriteLine(oVersion.ListItem["Title"]);

                }

            }

In a crooked way we can take that value by just restoring the item’s version to the current approved version.

oVersion.ListItem.File.Versions.Restore();

 

You can even do that from UI, see the first image and in the ECB menu we can see a Restore option. But in that case we will lose our currently updated data (In this case we will lose the metadata of Version 6.0).

 

This type of requirement will come after the content deployment. Sometimes we want to keep only the current approved version item metadata in the targeted list. So, I have checked lots of properties and methods but I couldn’t get a proper way to get the metadata information of the current approved version. At last I was able to get that information by just accessing the version with the index of of the version number and the column name.

 

oListItem.Versions[iVersionCount]["Title"] J

 

This is sample code for .NET console based application.

<code>

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SharePoint;

using System.Collections;

 

namespace GetListItemVersionInformation

{

    class Program

    {

        static void Main(string[] args)

        {

            SPSite oSite = new SPSite("https://sigr8-1b:1000/sites/mytestsite");

            SPWeb oWeb = oSite.OpenWeb();

            SPList oList = oWeb.Lists["Test1"];

            SPListItem oListItem = oList.Items.GetItemById(1);

            SPListItemVersionCollection oVersionInfo = oListItem.Versions; 

            int iVersionCount=0;

            foreach (SPListItemVersion oVersion in oVersionInfo)

            {

                if (oVersion.Level == SPFileLevel.Published)

                {

  // here I am retrieving all metadata properties of this item’s current approved version.

                    // after taking these values you can export it to anywhere.

                    Console.WriteLine(oListItem.Versions[iVersionCount]["Title"].ToString());

                    Console.ReadLine();

                    return;// after getting the latest current approved version we are exiting from the code

                }

                iVersionCount++;

            }

        }

    }

}

</code>

 

Happy coding C