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.
how to show version history of sharepoint in list items?
many thanks to yr solutions
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?”
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
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.