Destroying page and object version history

   —   

In this article you can find a short code sample that clears the page version history as well as the objects version history.

In some cases you may need to clear your database content, for example after a development or testing phase.

Credits go to Chris Meagher from Ridgeway who shared this with us.

Be sure you have your DB backups ready. This code is just proof of concept and will destroy the versioning data. The example does not take into account pages/objects that are currently checked out or in the recycle bin.The only option to get these records back is to do a DB restore.

You can download a zip package of the necessary files here:

DestroyVersionHistory.zip

Below is the contents of these files for your review:

DestroyVersionHistory.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DestroyVersionHistory.aspx.cs" Inherits="CMSApp.utils.DestroyVersionHistory" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:Button runat="server" Text="Delete Document History" OnClick="DeleteDocumentHistory_Click" /> <asp:Button runat="server" Text="Delete Object History" OnClick="DeleteObjectHistory_Click" /> </form> </body> </html>

DestroyVersionHistory.aspx.cs:

using CMS.DataEngine; using CMS.DocumentEngine; using CMS.Helpers; using CMS.Membership; using CMS.Synchronization; using CMS.UIControls; using System; using System.Linq; namespace CMSApp.utils { public partial class DestroyVersionHistory : CMSPage { protected void Page_Load(object sender, EventArgs e) { var user = MembershipContext.AuthenticatedUser; if (!user.IsGlobalAdministrator) { // This page should only be accessible to global admins RequestHelper.Respond404(); return; } } private void DeleteDocumentHistory(UserInfo user) { // Get ids of all documents that have version information var docQuery = new DataQuery() .From("CMS_VersionHistory") .Column("DocumentID") .Distinct(); var docIds = docQuery .Select(row => int.Parse(row[0].ToString())) .ToArray(); // Use the version manager to destroy document history var tree = new TreeProvider(user); var versionManager = VersionManager.GetInstance(tree); foreach (int docId in docIds) { versionManager.DestroyDocumentHistory(docId); } } private void DeleteObjectHistory(UserInfo user) { // Get ids and types of all objects that have version information var objectQuery = new DataQuery() .From("CMS_ObjectVersionHistory") .Columns("VersionObjectType", "VersionObjectID") .Distinct(); var objectVersions = objectQuery .Select(row => new { ObjectType = row[0].ToString(), ObjectId = int.Parse(row[1].ToString()) }) .ToArray(); // Destroy object history foreach (var objectVersion in objectVersions) { ObjectVersionManager.DestroyObjectHistory(objectVersion.ObjectType, objectVersion.ObjectId); } } protected void DeleteDocumentHistory_Click(object sender, EventArgs e) { var user = MembershipContext.AuthenticatedUser; DeleteDocumentHistory(user); } protected void DeleteObjectHistory_Click(object sender, EventArgs e) { var user = MembershipContext.AuthenticatedUser; DeleteObjectHistory(user); } } }

Share this article on   LinkedIn Google+

Juraj Ondrus

Hi, I am the Technical support leader at Kentico. I'm here to help you use Kentico and get as much as possible out of it.