Kentico CMS 7.0 Developer's Guide

Object versioning

Object versioning

Previous topic Next topic Mail us feedback on this topic!  

Object 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 creates a versioned CSS stylesheet.

 

private bool CreateVersionedObject()

{

  // Create new css stylesheet object

   CssStylesheetInfo newStylesheet = new CssStylesheetInfo();

 

  // Check if object versioning of stylesheet objects is allowed on current site

  if (ObjectVersionManager.AllowObjectVersioning(newStylesheet))

   {

      // Set the properties

       newStylesheet.StylesheetDisplayName = "My new versioned stylesheet";

       newStylesheet.StylesheetName = "MyNewVersionedStylesheet";

       newStylesheet.StylesheetText = "Some versioned CSS code";

 

      // Save the css stylesheet

       CssStylesheetInfoProvider.SetCssStylesheetInfo(newStylesheet);

 

      // Add css stylesheet to site

      int stylesheetId = newStylesheet.StylesheetID;

      int siteId = CMSContext.CurrentSiteID;

 

       CssStylesheetSiteInfoProvider.AddCssStylesheetToSite(stylesheetId, siteId);

 

      return true;

   }

 

  return false;

}

 

The following example creates a new version of the CSS stylesheet created by the example above.

 

private bool CreateVersion()

{

  // Get the css stylesheet

   CssStylesheetInfo newStylesheetVersion = CssStylesheetInfoProvider.GetCssStylesheetInfo("MyNewVersionedStylesheet");

  if (newStylesheetVersion != null)

   {

      // Check if object versioning of stylesheet objects is allowed on current site

      if (ObjectVersionManager.AllowObjectVersioning(newStylesheetVersion))

       {

          // Update the properties

           newStylesheetVersion.StylesheetDisplayName = newStylesheetVersion.StylesheetDisplayName.ToLower();

 

          // Create new version

           ObjectVersionManager.CreateVersion(newStylesheetVersion, true);

 

          return true;

       }

   }

 

  return false;

}

 

The following example performs rollback of the CSS stylesheet to the original version.

 

private bool RollbackVersion()

{

  // Get the css stylesheet

   CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("MyNewVersionedStylesheet");

  if (stylesheet != null)

   {

      // Prepare query parameters

      string where = "VersionObjectID =" + stylesheet.StylesheetID + " AND VersionObjectType = '" + stylesheet.ObjectType + "'";

      string orderBy = "VersionModifiedWhen ASC";

      int topN = 1;

 

      // Get dataset with versions according to the parameters

       DataSet versionDS = ObjectVersionHistoryInfoProvider.GetVersionHistories(where, orderBy, topN, null);

 

      if (!DataHelper.DataSourceIsEmpty(versionDS))

       {

          // Get version

           ObjectVersionHistoryInfo version = new ObjectVersionHistoryInfo(versionDS.Tables[0].Rows[0]);

 

          // Roll back

           ObjectVersionManager.RollbackVersion(version.VersionID);

 

          return true;

       }

   }

 

  return false;

}

 

The following example destroys the latest version of the CSS stylesheet.

 

private bool DestroyVersion()

{

  // Get the css stylesheet

   CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("MyNewVersionedStylesheet");

  if (stylesheet != null)

   {

      // Get the latest version

       ObjectVersionHistoryInfo version = ObjectVersionManager.GetLatestVersion(stylesheet.ObjectType, stylesheet.StylesheetID);

 

      if (version != null)

       {

          // Destroy the latest version

           ObjectVersionManager.DestroyObjectVersion(version.VersionID);

 

          return true;

       }

   }

 

  return false;

}

 

The following example destroys the whole version history of the CSS stylesheet.

 

private bool DestroyHistory()

{

  // Get the css stylesheet

   CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("MyNewVersionedStylesheet");

  if (stylesheet != null)

   {

      // Destroy version history

       ObjectVersionManager.DestroyObjectHistory(stylesheet.ObjectType, stylesheet.StylesheetID);

 

      return true;

   }

 

  return false;

}

 

The following example ensures that the CSS stylesheet has at least one version (if it does not have one, it creates it).

 

private bool EnsureVersion()

{

  // Get the css stylesheet

   CssStylesheetInfo stylesheet = CssStylesheetInfoProvider.GetCssStylesheetInfo("MyNewVersionedStylesheet");

  if (stylesheet != null)

   {

      // Check if object versioning of stylesheet objects is allowed on current site

      if (ObjectVersionManager.AllowObjectVersioning(stylesheet))

       {

          // Ensure version

           ObjectVersionManager.EnsureVersion(stylesheet, false);

 

          return true;

       }

   }

 

  return false;

}