How to remove newly created item version if there is no change with previous version in sitecore programmatically

471 views Asked by At

I am creating version of item on lock & edit button. I want to remove that version if author does not change any value of that version while check in version. I would like to compare versions and delete newly created version if there is no change while check in item version.

Note: No workflow is needed

2

There are 2 answers

0
Helga On BEST ANSWER

On ItemSaving event you can get the list of changes in the item. Here is some sample code to get the idea:

protected void OnItemSaving(object sender, EventArgs args)
    {
        var newItem = Event.ExtractParameter(args, 0) as Item;
        Item originalItem = newItem.Database.GetItem(newItem.ID, newItem.Language, newItem.Version);
    var differences = FindDifferences(newItem, originalItem);    
} 

private List<string> FindDifferences(Item newItem, Item originalItem)
    {
        newItem.Fields.ReadAll();
        IEnumerable<string> fieldNames = newItem.Fields.Select(f => f.Name);
        return fieldNames
          .Where(fieldName => newItem[fieldName] != originalItem[fieldName])
          .ToList();
    }
1
Anton On
  1. Open item in Content Editor
  2. Click on Versions tab
  3. Click on Compare button on Versions chunk As result you will see dialog with ability to compare 2 versions. If you don't detect any changes then you can remove newly created version. enter image description here P.S. Screenshot is from 7.2 version, but 8+ version have pretty the same way to compare versions.