Search code examples
sitecoresitecore8

Sitecore compare items version programmatically


Lets suppose I have an Item with 5 versions. (I am not talking about language versions).

I would like to compare all the versions to identify the differencies. Is there some thing available out of box from Sitecore to do this task? Or I have have to loop over all the versions and then all the fields to find the differences.


Solution

  • No, there is nothing like that out of the box. You will have to compare fields one by one.

    Remember that some of the fields should be ignored (like __Updated, __ValidFrom, __Workflow State, etc).

    Remember that it's not easy to display what was changed in Blob fields.

    And here is a code for you to start with:

    FieldCollection fields = version1.Fields;
    fields.ReadAll();
    fields.Sort();
    foreach (Field field1 in fields)
    {
        if (field1.ShouldBeTranslated)
        {
            Field field2 = version2.Fields[field1.ID];
            var value1 = field1.Value;
            var value2 = field2.Value;
            ... // whatever you need here
    

    Make sure you add all the necessary null checks! I skipped them for the clarity.