We ran into som problems on a Sitecore Intranet Portal installation a few weeks ago, and i just thought i would blog a little about it, to help others that might be in the same situation later.
The issue was found on Sitecore Intranet Portal, but will reside in a normal Sitecore installation. We had a few rutines on the installation that was adding new versions of items, when they was updated, this was done so the editors could go back to an earlier version, whenever they wanted.
We saw the problem, when some pages was staring to render very slowly, and we couldnt find a proper solution. After som heavy investergating, we found that there were many items that were containing more than 200 versions!. It seems that this could slow Sitecore down, and especialy the SIP, as it has a feature that allows the normal user to see the version history, including date and author.
Our solution to the problem was to implement a feature that deletes old versions, if the item has many versions, we set the limit at 10 versons.
The code is added to the OnVersionAdded event and looks like the following:
public void OnVersionAdded(object sender, EventArgs args) { Item createdItem = Event.ExtractParameter(args, 0) as Item; int allowedItemVersions = 0; if(int.TryParse(Settings.GetSetting("AllowedItemVersions"), out allowedItemVersions)) { if (createdItem == null) return; if (createdItem.Versions.Count > allowedItemVersions) { Item item = Factory.GetDatabase("master").GetItem(createdItem.ID); using (new SecurityDisabler()) { RemoveOldVersions(allowedItemVersions, createdItem, item); } } } } private void RemoveOldVersions(int allowedItemVersions, Item createdItem, Item latestPublishedWorkflowVersion) { int currentVersionIndex = 0; int versionsDeleted = 0; while (createdItem.Versions.Count > allowedItemVersions) { if (createdItem.Versions.GetVersionNumbers().Length > currentVersionIndex) { var currentVersion = createdItem.Versions[createdItem.Versions.GetVersionNumbers()[currentVersionIndex]]; if (latestPublishedWorkflowVersion != null) { if (currentVersion.Version.Number != latestPublishedWorkflowVersion.Version.Number) { currentVersion.Versions.RemoveVersion(); versionsDeleted++; } else { currentVersionIndex++; } } else { currentVersion.Versions.RemoveVersion(); versionsDeleted++; } } } LogUtil.Info("RemoveOldVersions - Removed " + versionsDeleted + " versions from " + createdItem.Name); }
The code relies on a Sitecore setting caled AllowedItemVersions that defines how many versions should be keept.
We started by running the above script on all items in the solution, to clean every item up, and afterwards, we wired the event up, so every tie a new version is added, itll clean up old versions.
Reference used to develop the feature, of curse from John West :-) http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2011/06/Remove-Old-Versions-of-Items-in-the-Sitecore-ASPNET-CMS.aspx