Thursday, November 11, 2021

Sitecore CM: Limiting the number of versions in Sitecore

" If you found yourself searching for a solution for limiting the number of versions that get created by your content authors, you probably have come across the old blog post by John West (Rules Engine Actions to Remove Old Versions in the Sitecore ASP.NET CMS). The blog post is awesome like all blog posts that John has written. It is over 8 years ago and some things did change in Sitecore since then. So I figured I would write an update to it as I wasn't able to find one.

The code part is still valid. I did run into an exception in the "while" loop, so I changed the code in the Apply function to the following:

public override void Apply(T ruleContext) { 

    Assert.ArgumentNotNull(ruleContext, "ruleContext");     

    Assert.ArgumentNotNull(ruleContext.Item, "ruleContext.Item"); 

    // for each language available in the item 

    foreach (Language lang in ruleContext.Item.Languages) { 

        var item = ruleContext.Item.Database.GetItem(ruleContext.Item.ID, lang); 

        if (item == null) { continue; } 

        var versionsToProcess = item.Versions.GetOlderVersions().OrderByDescending(i => i.Version.Number).Skip(this.MinVersions); 

        foreach(var version in versionsToProcess) { 

            Assert.IsNotNull(version, "version"); 

            if (this.MinUpdatedDays < 1 || version.Statistics.Updated.AddDays(this.MinUpdatedDays) < DateTime.Now) {

                this.HandleVersion(version); 

            

        

    

}

There are a few things changed in the Rules area.

After you create all four classes for the rule actions (MinVersionsAction, RecycleOldVersions, ArchiveOldVersions, and DeleteOldVersions) the following actions will need to be performed in Sitecore:

  • Go to "/sitecore/system/Settings/Rules/Definitions/Elements" and duplicate any of the elements. Name the new element "Item Version Limiting" or any other way you choose.
  • Remove all conditions and actions if you duplicated an existing element that had them.
  • Create three Actions using Action data template as John described: Archive Versions Beyond, Delete Versions Beyond, Recycle Versions Beyond.
  • In the Text field put the values that John provided:
Archive Versions Beyond:
archive versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days

Delete Version Beyond:
delete versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days

Recycle Versions Beyond:
recycle versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days
  • In the Type field put the reference to the corresponding class you created.

  • To get the actions to appear in the rule selection window you'll have to add a new Tag called "Item Version Limiting" under "/sitecore/system/Settings/Rules/Definitions/Tags/Item Version Limiting". You can duplicate an existing tag for that.
  • In "Default" item under "/sitecore/system/Settings/Rules/Definitions/Elements/Item Version Limiting/Tags" update the reference to the newly created tag.

  • In the Default item under "/sitecore/system/Settings/Rules/Item Saved/Tags" in the Tags field add a reference to the newly created "Item Version Limiting" tag.

  • Go to the "/sitecore/system/Settings/Rules/Item Saved/Rules" and add a new rule. Define the rule condition and action in the Rules field.



Condition is mandatory and the most suitable one I found without writing my own was the "where the number of versions compares to number" one from the Item Version CM.


No comments:

Post a Comment