Kentico CMS 6.0 Developer's Guide

Creating documents

Creating documents

Previous topic Next topic Mail us feedback on this topic!  

Creating documents

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 new document under workflow.

 

private bool CreateDocument()

{

  // 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 parentNode = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (parentNode != null)

   {

      // Create a new node

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

 

      // Set the required document properties

       node.DocumentName = "My new document";

       node.DocumentCulture = "en-us";

 

      // Insert the document

      DocumentHelper.InsertDocument(node, parentNode, tree);

 

      return true;

   }

 

  return false;

}

 

The following example creates a new culture version of the document under workflow created by the example above.

 

private bool CreateNewCultureVersion()

{

  // 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 document in the English culture

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

 

  if (node != null)

   {

      // Translate its name

       node.DocumentName = "My new translation";

       node.SetValue("MenuItemName", "My new translation");

 

       node.DocumentCulture = "de-de";

 

      // Insert into database

      DocumentHelper.InsertNewCultureVersion(node, tree);

 

      return true;

   }

 

  return false;

}

 

The following example creates a new linked document based on the document under workflow created by the first code example on this page.

 

private bool CreateLinkedDocument()

{

  // 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 parentNode = DocumentHelper.GetDocument(siteName, aliasPath, culture, combineWithDefaultCulture, classNames, where, orderBy, maxRelativeLevel, selectOnlyPublished, columns, tree);

 

  if (parentNode != null)

   {

      // Change the alias path

       aliasPath += "/My-new-document";

 

      // Get the original document

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

 

      if (node != null)

       {

          // Insert the link

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

 

          return true;

       }

      else

       {

           apiCreateLinkedDocument.ErrorMessage = "Document 'My new document' was not found.";

       }

   }

 

  return false;

}