Sunil Aher
Sunil Aher

Reputation: 769

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

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

Upvotes: 1

Views: 497

Answers (2)

Helga
Helga

Reputation: 56

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();
    }

Upvotes: 1

Anton
Anton

Reputation: 9961

  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.

Upvotes: 0

Related Questions