Sunday, April 30, 2017

Sitecore Version Pruner Command

Sitecore Version Pruner Command:

It is just a scheduled task that you can configure to remove old/unused versions from your content tree.

  • Install the appropriate Sitecore package
  • In Content Editor, navigate to: /sitecore/System/Tasks/Commands/Run Version Pruner
  • Set the "Root" field to the item from which to start pruning versions. Change other fields as required.
  • In Content Editor, navigate to: /sitecore/System/Tasks/Schedules/Version Pruner Schedule
  • Modify the "Schedule" field to your desired run scheduled. 

 




How to override Sitecore Version Pruner Command –
 


[Serializable]
    public class VersionPrunerCommand : Sitecore.SharedSource.VersionPruner.Tasks.VersionPrunerCommand
    {
        protected override void ProcessItemTree(Item item)
        {
            if (Sitecore.Context.Job != null)
            {
                Sitecore.Context.Job.Status.Processed++;
            }

            // Get the item in all languages
            foreach (var itemLanguage in item.Languages)
            {
                var languageItem = item.Database.GetItem(item.ID, itemLanguage);
                if (languageItem.Versions.Count > 0)
                {
                    Sitecore.Context.Job.Status.Messages.Add("processing: " + item.Paths.Path + ", " + item.Language.Name);

                    // Run item against the Item Filter rule(s)
                    var ruleContext = new RuleContext();
                    ruleContext.Item = languageItem;
                    this.ItemFilterRules.Run(ruleContext);

                    if (ruleContext.Parameters.ContainsKey("ItemValidForVersionRemoval"))
                    {
                        // Rule was passed, so this item's versions should be trimmed..
                        TrimItemVersions(languageItem);
                    }
                }
            }

            // Process all descendant items
            foreach (Item child in item.Children)
            {
                ProcessItemTree(child);
            }
        }
    }


I hope you like this. Stay tuned for more Sitecore related articles.

Happy Sitecoring :)

Please leave your comments or share this tip if it’s useful for you.