Revision history of content items

Delete revision history for content items

The following code sample demonstrates how to delete the entire revision history for a content item:

C#
using System;
using System.Linq;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Versioning;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.RevisionHistory
{
   public partial class RevisionHistorySnippets
   {
       public void DeleteEntireRevisionHistoryForNewsItem(Guid newsItemMasterId)
       {
           // Get the news manager and find the news item
           NewsManager newsManager = NewsManager.GetManager();
           NewsItem newsItem = newsManager.GetNewsItem(newsItemMasterId);

           // Get the version manager
           VersionManager versionManager = VersionManager.GetManager();

           // Get all the changes for the news item
           // except last published version
           var changes = versionManager.GetItemVersionHistory(newsItem.Id).Where(h => !h.IsLastPublishedVersion);

           // Iterate through all the changes
           foreach (var change in changes)
           {
               // Delete the current change
               versionManager.DeleteChange(change.Id);
           }

           // Save the changes
           versionManager.SaveChanges();
       }
   }
}

In the code above, you get an instance of the VersionManager class. Next, you call the GetItemVersionHistory method of the version manager and pass the ID of the content item. As a result, all revision history changes for that specific content item are queried. To delete these changes, you iterate through the queried changes and call the DeleteChange method of the version manager by passing the ID of each of the changes``. Finally, you save the changes of the operation.

Delete revision history up to a specific point in time

The following code sample demonstrates how to delete the revision history of a content item up to a specific date:

C#
using System;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Versioning;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.RevisionHistory
{
   public partial class RevisionHistorySnippets
   {
       public void DeleteRevisionHistoryForNewsItem(Guid newsItemMasterId, DateTime date)
       {
           // Get the news manager and find the news item
           NewsManager newsManager = NewsManager.GetManager();
           NewsItem newsItem = newsManager.GetNewsItem(newsItemMasterId);

           // Get the version manager
           VersionManager versionManager = VersionManager.GetManager();

           // Delete all the changes with dates older or equal to the specified date
           versionManager.TruncateVersions(newsItem.Id, date);

           // Save the changes
           versionManager.SaveChanges();
       }
   }
}

In the code above, you get an instance of the VersionManager class. To delete the revision history up to a specific date, you call the TruncateVersions method of the version manager and pass the ID of the content item and the date up to which you want to delete the revision history. Finally, you save the changes of the operation.

Delete all but a specific number of revision history changes

The following code sample demonstrates how to delete all but an exact number of revision history changes, for example, keep just the last 10 modifications you made to a content item and delete all other changes:

C#
using System;
using System.Linq;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Versioning;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.RevisionHistory
{
   public partial class RevisionHistorySnippets
   {
       public void DeleteRevisionHistoryForNewsItem(Guid newsItemMasterId, int revisionsToRemain)
       {
           // Get the news manager and find the news item
           NewsManager newsManager = NewsManager.GetManager();
           NewsItem newsItem = newsManager.GetNewsItem(newsItemMasterId);

           // Get the version manager
           VersionManager versionManager = VersionManager.GetManager();

           // Get the changes for the news item
           var changes = versionManager.GetItemVersionHistory(newsItem.Id);

           // Order the changes by version number and Skip by the
           // amount you wish to remain in the Version History
           var changeToRemove = changes
               .OrderByDescending(c => c.Version)
               .Skip(revisionsToRemain)
               .FirstOrDefault();

           // Check if a change is found that satisfies the filtering conditions
           if (changeToRemove != null)
           {
               // Delete all changes with version number smaller or equal to the specified number
               versionManager.TruncateVersions(newsItem.Id, changeToRemove.Version);

               // Save the changes
               versionManager.SaveChanges();
           }
       }
   }
}

In the code above, you call the GetItemVersionHistory method of the version manager and pass the ID of the content item. As a result, all the revision history changes for that specific content item are queried. Next, you order the queried revision history changes by Version number. During this step, you apply the filtering conditions defining how many revision history changes to skip. When calling the TruncateVersions method of the version manager, you pass the ID of the content item and a Version number. As a result, all revision history changes with version smaller or equal to the specified number are deleted.

Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.