Revision history of pages
Delete revision history for pages
The following code demonstrates how to delete the revision history for all pages up to a specified date:
using System;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Versioning;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.RevisionHistory
{
public partial class RevisionHistorySnippets
{
public void DeleteRevisionHistoryForPages(DateTime date)
{
// Get the page manager and all the pages
var pageManager = PageManager.GetManager();
var pages = pageManager.GetPageNodes();
// Get the version manager
var versionManager = VersionManager.GetManager();
// Iterate through all the pages
foreach (PageNode page in pages)
{
// Check the number of changes for the current page
if (versionManager.GetItemVersionHistory(page.PageId).Count >= 1)
{
// Delete all the changes with dates older or equal to the specified date
versionManager.TruncateVersions(page.PageId, date);
}
}
// Save the changes
versionManager.SaveChanges();
}
}
}
In the code above, you iterate through all the page nodes. By calling the TruncateVersions method of the version manager, you can delete the revision history of a page. You do this by passing a page ID and a date up to which you want to delete the changes. Finally, you save the changes.
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 page and delete all other changes:
using System.Linq;
using Telerik.Sitefinity.Modules.Pages;
using Telerik.Sitefinity.Pages.Model;
using Telerik.Sitefinity.Versioning;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.RevisionHistory
{
public partial class RevisionHistorySnippets
{
public void DeleteRevisionHistoryForPages(int revisionsToRemain)
{
// Get the page manager and all the pages
var pageManager = PageManager.GetManager();
var pages = pageManager.GetPageNodes();
// Get the version manager.
var versionManager = VersionManager.GetManager();
// Iterate through all the pages
foreach (PageNode page in pages)
{
// Get the changes for the current page
var changes = versionManager.GetItemVersionHistory(page.PageId);
// 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(page.PageId, changeToRemove.Version);
}
}
// Save the changes
versionManager.SaveChanges();
}
}
}
In the code above, you iterate through all the page nodes. By calling the GetItemVersionHistory method of the version manager, you query all the revision history changes for a particular page. 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.