Kentico CMS 6.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 MoveDocument()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Prepare parameters

  string siteName = CMSContext.CurrentSiteName;

  string aliasPath = "/API-Example/My-new-document";

  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);

 

   aliasPath = "/API-Example/Target";

 

  // Get the new parent document

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

 

  if ((node != null) && (parentNode != null))

   {

      // Move the document

      DocumentHelper.MoveDocument(node, parentNode.NodeID, tree);

 

      return true;

   }

 

  return false;

}