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:

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:

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:

 

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?

Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.

Get started with Integration Hub | Sitefinity Cloud | Sitefinity SaaS

This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.

Web Security for Sitefinity Administrators

This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.

Foundations of Sitefinity ASP.NET Core Development

The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.

Was this article helpful?