This blog post provides a base and three prototype implementations of actions for the Sitecore ASP.NET web Content Management System (CMS) rules engine that take some action on older versions beyond a threshold that you pass as a parameter to the action. Pruning unnecessary versions can improve performance and usability for CMS users.
The abstract base class implements the Apply() method to determine the old versions to process, and calls the HandleVersion() method for each. Rules engine actions that derive from this abstract base class implement the HandleVersion() method.
The abstract base class implements two properties:
Passing these two parameters to the action seems a little inconsistent because logic involving parameters generally belongs in conditions. This approach is appropriate in this case because it avoids the need to pass any data from the condition to the action.
The three concrete classes are:
You can implement an OnItemSaved rule to invoke this logic every time a user saves an item and/or run a rule against all items to invoke one of these actions.
namespace
Sitecore.Sharedsource.Rules.Actions
{
using
System;
Assert = Sitecore.Diagnostics.Assert;
SC = Sitecore;
public
abstract
class
MinVersionsAction<T> :
SC.Rules.Actions.RuleAction<T> where T : SC.Rules.RuleContext
/// <summary>
/// Backs the MinVersions property.
/// </summary>
private
int
_minVersions;
/// Gets or sets a value that indicates
/// the minimum number of versions to retain.
/// Set by rule parameters, or defaults to 30.
MinVersions
get
return
this
._minVersions < 1 ? 30 :
._minVersions;
}
set
._minVersions = value;
/// Gets or sets a value that indicates the minimum age of versions to
/// remove, in days. Actions that derive from this abstract base class
/// will not process Versions updated within this number of days.
MinUpdatedDays {
;
; }
/// Apply the rule.
/// <param name="ruleContext">Rule processing context.</param>
override
void
Apply(T ruleContext)
Assert.ArgumentNotNull(ruleContext,
"ruleContext"
);
Assert.ArgumentNotNull(ruleContext.Item,
"ruleContext.Item"
// for each language available in the item
foreach
(SC.Globalization.Language lang
in
ruleContext.Item.Languages)
SC.Data.Items.Item item = ruleContext.Item.Database.GetItem(
ruleContext.Item.ID,
lang);
if
(item ==
null
)
continue
// to prevent the while loop from reaching MinVersions,
// only process this number of items
limit = item.Versions.Count -
.MinVersions;
i = 0;
while
(item.Versions.Count >
.MinVersions && i < limit)
SC.Data.Items.Item version = item.Versions.GetVersions()[i++];
Assert.IsNotNull(version,
"version"
(
.MinUpdatedDays < 1
|| version.Statistics.Updated.AddDays(
.MinUpdatedDays) < DateTime.Now)
.HandleVersion(version);
/// Classes that derive from this abstract base class
/// implement this method to remove versions.
/// <param name="version">The version to process.</param>
HandleVersion(Sitecore.Data.Items.Item version);
/// Rules engine action that recycles old versions of items.
RecycleOldVersions<T> :
MinVersionsAction<T> where T : SC.Rules.RuleContext
/// Recycle the old version.
/// <param name="version">The old version to recycle.</param>
HandleVersion(SC.Data.Items.Item version)
Assert.ArgumentNotNull(version,
SC.Diagnostics.Log.Audit(
,
"Recycle version : {0}"
new
string
[] { SC.Diagnostics.AuditFormatter.FormatItem(version) });
version.RecycleVersion();
/// Rules engine action that archives old versions of items.
ArchiveOldVersions<T> :
/// Archives the old version.
/// <param name="version">The old version to archive.</param>
SC.Data.Archiving.Archive archive = version.Database.Archives[
"archive"
];
(archive !=
object
(),
"Archive version: {0}"
archive.ArchiveVersion(version);
else
/// Rules engine action that deletes old versions of items.
DeleteOldVersions<T> : MinVersionsAction<T> where T : SC.Rules.RuleContext
/// Deletes the old version.
/// <param name="version">The old version to delete.</param>
"Delete version : {0}"
version.Versions.RemoveVersion();
To register the actions, in the Content Editor:
recycle versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days
archive versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days
delete versions beyond [MinVersions,positiveinteger,,number] older than [MinUpdatedDays,positiveinteger,,number] days
To create a rule that runs each time a user saves an item, in the Content Editor:
As always, thanks John, good information here. I think you wrote/blogged about a task to do something similar to this which we've implemented and use in all of our installations nowadays. We usually allow up to 10 versions. We started doing this after we had a client with 100+ versions for a few translated items on a multiple language home page, it was nearly unloadable until we were able to clip some of the old versions.