Tuesday, October 29, 2013

Kentico: Running API Examples from within CMS Desk

If API Examples is installed, the examples can be run from Support –> API examples.

Many of the examples have been hard coded with culture “en-us”, they will be most likely failed if the site is using a culture different from “en-us”.

The example code is like below, it fails on the red line:

#region "API examples - Creating documents"

   /// <summary>
   /// Creates the initial document strucutre used for the example. Called when the "Create document structure" button is pressed.
   /// </summary>
   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;
   }

You can modify the culture to your site’s culture and the example should be able to run on your site.