Kentico CMS 7.0 Developer's Guide

Sample documents and objects

Sample documents and objects

Previous topic Next topic Mail us feedback on this topic!  

Sample documents and objects

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 sample documents structure and workflow objects required for the API examples on the following pages to be executed.

 

private bool CreateExampleObjects()

{

  // Add a new culture to the current site

  CultureInfo culture = CultureInfoProvider.GetCultureInfo("de-de");

  CultureSiteInfoProvider.AddCultureToSite(culture.CultureID, CMSContext.CurrentSiteID);

 

  // Create a new tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the root node

  TreeNode parent = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/", "en-us");

 

  if (parent != null)

   {

      // Create the API example folder

      TreeNode node = TreeNode.New("CMS.Folder", tree);

 

       node.DocumentName = "API Example";

       node.DocumentCulture = "en-us";

 

      // Insert it to database

      DocumentHelper.InsertDocument(node, parent, tree);

 

       parent = node;

 

      // Create the Source folder for moving

       node = TreeNode.New("CMS.Folder", tree);

 

       node.DocumentName = "Source";

       node.DocumentCulture = "en-us";

 

      DocumentHelper.InsertDocument(node, parent, tree);

 

      // Create the Target folder for moving

       node = TreeNode.New("CMS.Folder", tree);

 

       node.DocumentName = "Target";

       node.DocumentCulture = "en-us";

 

      DocumentHelper.InsertDocument(node, parent, tree);

 

      // Get the default workflow

      WorkflowInfo workflow = WorkflowInfoProvider.GetWorkflowInfo("default");

 

      if (workflow != null)

       {

          // Get the example folder data

           node = DocumentHelper.GetDocument(parent, tree);

 

          // Create new workflow scope

          WorkflowScopeInfo scope = new WorkflowScopeInfo();

 

          // Assign to the default workflow and current site and set starting alias path to the example document

           scope.ScopeWorkflowID = workflow.WorkflowID;

           scope.ScopeStartingPath = node.NodeAliasPath;

           scope.ScopeSiteID = CMSContext.CurrentSiteID;

 

          // Save the scope into the database

          WorkflowScopeInfoProvider.SetWorkflowScopeInfo(scope);

 

          return true;

       }

      else

       {

           apiCreateExampleObjects.ErrorMessage = "The default workflow was not found.";

       }

   }

 

  return false;

}

 

The following example deletes the sample document structure created by the example above, including the linked and copied documents created by executing the examples on the following pages.

 

private bool DeleteDocuments()

{

  // Create new tree provider

  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 example folder

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

 

  if (node != null)

   {

      // Prepare delete parameters

      bool deleteAllCultures = true;

      bool destroyHistory = true;

      bool deleteProduct = false;

 

      // Delete all culture versions of the document and destroy its version history. This method also destroys all child documents.

      DocumentHelper.DeleteDocument(node, tree, deleteAllCultures, destroyHistory, deleteProduct);

 

      return true;

   }

 

  return false;

}

 

The following example deletes the sample workflow objects created by the first example on this page.

 

private bool DeleteObjects()

{

  CultureInfo culture = CultureInfoProvider.GetCultureInfo("de-de");

 

  // Remove the example culture from the site

  CultureSiteInfoProvider.RemoveCultureFromSite(culture.CultureID, CMSContext.CurrentSiteID);

 

  // Prepare parameters

  string where = "ScopeStartingPath LIKE '/API-Example%'";

  string orderBy = null;

  int topN = 0;

  string columns = null;

 

  DataSet scopes = WorkflowScopeInfoProvider.GetWorkflowScopes(where, orderBy, topN, columns);

 

  if (!DataHelper.DataSourceIsEmpty(scopes))

   {

      // Loop through all the scopes in case more identical scopes were accidentally created

      foreach (DataRow scopeRow in scopes.Tables[0].Rows)

       {

          // Create scope info object

          WorkflowScopeInfo scope = new WorkflowScopeInfo(scopeRow);

 

          // Delete the scope

           scope.Delete();

       }

 

      return true;

   }

 

  return false;

}