Kentico CMS 6.0 Developer's Guide

Versioning

Versioning

Previous topic Next topic Mail us feedback on this topic!  

Versioning

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example roll back the document created in the Sample documents and objects topic back a specified version. Please note that the document needs to have at least one previous version for this example to be functional.

 

private bool RollbackVersion()

{

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example";

  string culture = "en-us";

  bool combineWithDefaultCulture = false;

  string classNames = TreeProvider.ALL_CLASSNAMES;

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

  string columns = null;

 

  // Get the document

  TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (node != null)

   {

      // Prepare the WHERE condition for the oldest document version

       where = "DocumentID = " + node.DocumentID;

       orderBy = "ModifiedWhen ASC";

      int topN = 1;

 

      // Get the version ID

      DataSet versionHistory = VersionHistoryInfoProvider.GetVersionHistories(where, orderBy, topN, columns);

 

      if (!DataHelper.DataSourceIsEmpty(versionHistory))

       {

          // Create the Version history info object

          VersionHistoryInfo version = new VersionHistoryInfo(versionHistory.Tables[0].Rows[0]);

 

          VersionManager versionManager = new VersionManager(tree);

 

          // Roll back version

           versionManager.RollbackVersion(version.VersionHistoryID);

 

          return true;

       }

      else

       {

           apiRollbackVersion.ErrorMessage = "The document's version history is empty.";

       }

   }

 

  return false;

}

 

The following example deletes the latest version of the document created in the Sample documents and objects topic. Please note that the document needs to have at least one version for the example to be functional.

 

private bool DeleteVersion()

{

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example";

  string culture = "en-us";

  bool combineWithDefaultCulture = false;

  string classNames = TreeProvider.ALL_CLASSNAMES;

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

  string columns = null;

 

  // Get the document

  TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (node != null)

   {

      // Prepare the WHERE condition for the latest document version

       where = "DocumentID = " + node.DocumentID;

       orderBy = "ModifiedWhen DESC";

      int topN = 1;

 

      // Get the version ID

      DataSet versionHistory = VersionHistoryInfoProvider.GetVersionHistories(where, orderBy, topN, columns);

 

      if (!DataHelper.DataSourceIsEmpty(versionHistory))

       {

          // Create the Version history info object

          VersionHistoryInfo version = new VersionHistoryInfo(versionHistory.Tables[0].Rows[0]);

 

          VersionManager versionManager = new VersionManager(tree);

 

          // Delete the version

           versionManager.DestroyDocumentVersion(version.VersionHistoryID);

 

          return true;

       }

      else

       {

           apiDeleteVersion.ErrorMessage = "The document's version history is empty.";

       }

   }

 

  return false;

}

 

The following example deletes the entire version history of the document created in the Sample documents and objects topic.

 

private bool DestroyHistory()

{

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example";

  string culture = "en-us";

  bool combineWithDefaultCulture = false;

  string classNames = TreeProvider.ALL_CLASSNAMES;

  string where = null;

  string orderBy = null;

  int maxRelativeLevel = -1;

  bool selectOnlyPublished = false;

  string columns = null;

 

  // Get the document

  TreeNode node = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (node != null)

   {

      VersionManager versionManager = new VersionManager(tree);

 

      // Destroy the version history

       versionManager.DestroyDocumentHistory(node.DocumentID);

 

      return true;

   }

 

  return false;

}