Kentico CMS 7.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 document structure used by all the following code examples in this chapter.

 

private bool CreateDocumentStructure()

{

  // Add a new culture to the current site

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

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

 

  // Create new instance of the Tree provider

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get parent node

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

 

  if (parentNode != null)

   {

      // Create the API Example folder

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

 

       newNode.DocumentName = "API Example";

       newNode.DocumentCulture = "en-us";

 

       newNode.Insert(parentNode);

     

       parentNode = newNode;

 

      // Create the Source folder - the document to be moved will be stored here

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

 

       newNode.DocumentName = "Source";

       newNode.DocumentCulture = "en-us";

 

       newNode.Insert(parentNode);

 

      // Create the Target folder - a document will be moved here

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

 

       newNode.DocumentName = "Target";

       newNode.DocumentCulture = "en-us";

 

       newNode.Insert(parentNode);

 

      return true;

   }

 

  return false;

}

 

The following example creates a document.

 

private bool CreateDocument()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the parent node - the API Example folder

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

 

  if (parentNode != null)

   {

      // Create a new instance of the Tree node

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

 

      // Set the document's properties

       newNode.DocumentName = "My new document";

       newNode.DocumentCulture = "en-us";

 

      // Insert the document into the content tree

       newNode.Insert(parentNode);

 

      return true;

   }

 

  return false;

}

 

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

 

private bool CreateNewCultureVersion()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the document in the english culture

  TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example/My-new-document", "en-us");

 

  if (node != null)

   {

      // Translate its name

       node.DocumentName = "My new translation";

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

 

      // Insert into database

       node.InsertAsNewCultureVersion("de-de");

 

      return true;

   }

 

  return false;

}

 

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

 

private bool CreateLinkedDocument()

{

  // Create an instance of the Tree provider first

  TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

 

  // Get the parent document

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

 

  if (parentNode != null)

   {

      // Get the original document

      TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example/My-new-document", "en-us");

 

      if (node != null )

       {

          // Insert the link

           node.InsertAsLink(parentNode);

 

          return true;

       }

      else

       {

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

       }

   }

 

  return false;

}