Working with Versions in SharePoint List programmatically

There are very few posts on working with Versions in SharePoint Lists Programmatically. While browsing through those posts I ran in to a question saying “How to get the Columns which changed in the SharePoint List?”

Problem Description:

How to get only the Updated Columns in a SharePoint List with respect to Versions? or maybe get the columns changed from the previous version with respect to the current Version?

Solution:

ArrayList alUpdatedFields = new ArrayList();

bool bItemChanged = false;

SPListItemCollection objItemColl = objList.GetItems(objQuery);

SPListItem objItem = objItemColl[0];

SPListItemVersionCollection objVersionColl = objItem.Versions;

if (objVersionColl != null && objVersionColl.Count > 0)
{
foreach (SPListItemVersion item in objVersionColl)
{
if (item.VersionLabel.ToString() != objItem[“_UIVersionString”].ToString())
{
foreach (SPField objField in objItem.Fields)
{
if (!objField.ReadOnlyField && IsValueChanged(objField.Type, objItem[objField.InternalName], item[objField.InternalName]))
{
bItemChanged = true;
alUpdatedFields.Add(objField.Title);
}
}
if (bItemChanged)
{
break;
}
}
}
}

In the above code I am looking for the previously modified version compared to the current version and adding those fields to the Array List, instead we can have an HashTable to add the Field and its value as a key value pair.

if (item.VersionLabel.ToString() != objItem[“_UIVersionString”].ToString()) //This condition is used to ignore the comparison of the current version with itself. you can add your own logic to compare it with the actually needed version

Below is the function to check if the value for a particular field has changed compared to other version:

private bool IsValueChanged(SPFieldType type, object FirstValue, object SecondValue)
{
if (string.IsNullOrEmpty(Convert.ToString(FirstValue)) && string.IsNullOrEmpty(Convert.ToString(SecondValue)))
{
return false;
}
else if (string.IsNullOrEmpty(Convert.ToString(FirstValue)))
{
return true;
}
else if (string.IsNullOrEmpty(Convert.ToString(SecondValue)))
{
return true;
}

switch (type)
{
case SPFieldType.DateTime:
return !Convert.ToDateTime(FirstValue).Date.Equals(Convert.ToDateTime(Convert.ToString(SecondValue)).Date);
case SPFieldType.User:
break;
case SPFieldType.Text:
case SPFieldType.Note:
return !Convert.ToString(FirstValue).ToUpper().Equals(Convert.ToString(SecondValue).ToUpper());
case SPFieldType.Boolean:
return !Convert.ToBoolean(FirstValue).Equals(Convert.ToBoolean(SecondValue));
case SPFieldType.Attachments:
break;
default:
return !FirstValue.Equals(SecondValue);
}

return false;
}

*Note: I have executed the code for the first item in the List, instead we can have it as a loop for multiple items.

Advertisement
This entry was posted in Uncategorized. Bookmark the permalink.

8 Responses to Working with Versions in SharePoint List programmatically

  1. atan says:

    how to show version history of sharepoint in list items?
    many thanks to yr solutions

    • Venkat Karri says:

      Hi Atan,

      Go to your List –> Settings –> List Settings

      Once you get here Under the General Settings Sections Click Versioning Settings and then Select the Yes (Radio Button) for “Create a Version each time you edit an item in this List?”

      • Atan says:

        Dear Karri,
        Thanks for yr quickly response
        I can view the version history by clicking the version history after setting the above instruction. Besides that, pls suggest me how to show every “modified” in the list items (do not click the items). I need to show the time every modified in the list items. If I add modified to the list, the time of modified only show the last modified (I need to show of all them)

        Many Thanks for yr support
        Atan

  2. Venkat Karri says:

    Hi Atan,

    I am not sure if I understand your question: So you want to view “Modified” field for all the versions for all items at one glance? In that case you may have to a go for a customized form as each version is also treated as a Version Item (List Item) with all meta data.

  3. Ben says:

    Venkat,
    I am also trying to accomplish what I believe Atan is, in that I’m trying to build a view that acts as an ‘Activity Feed’ of sorts for a particular versioned list. So I’d like to chronologically list each version of each item in that list, so that I can see when all changes to the list items were made. I can’t seem to get more than the latest version of any given item to display though. I’m using Sharepoint 2013 Online. Is there any way you could point me in the right direction of a custom form or anything else that would accomplish this?

    Thanks!

    • Venkat Karri says:

      Ben,

      you can certainly build a custom page (could be ribbon item or a list page whose url refers to the custom page you will be building) and accomplish your requirement. My recommendation would be:

      Please refer to the below lines in the code:

      if (objVersionColl != null && objVersionColl.Count > 0)
      {
      foreach (SPListItemVersion item in objVersionColl)
      {
      }
      }

      This is where you can retrieve all the versions of the current item. Create a data table (you can use JQuery Datatables) and add each of the itemversion to this table.

      I would prefer JQuery Accordion which gives you group by option, where the groupby condition would be your List Item and the child nodes would be Versions of that particular item.

      sample table structure could be something like this:

      {{for}} — this is where it loops for each item

      {{Item Title}}

      {{for-each}} – this would be for each version for the current Item in the loop


      {{/for-each}}
      {{/for}}

      Hope this helps!!

      -Venkat

  4. Raihan Iqbal says:

    Hi,

    Can I achieve this using the entity classes generated by SPMetal?

    • Venkat Karri says:

      Not Sure, if you can achieve this completely using SPMetal. Personally I do not prefer using SPMetal because of its limitations with few SharePoint fields such as taxonomy fields and few other fields.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s