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.

About these ads
This entry was posted in Uncategorized. Bookmark the permalink.

5 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!

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s