Thursday, June 21

Dynamically creating folders and pages from the data store in Kentico CMS

Google+ Pinterest LinkedIn Tumblr +

This article explains how to create folder structure and pages in Kentico content tree using code. These kinds of situations are very common when we want to move an existing site to Kentico CMS.

Recently, we came across such a situation where we have to move the existing ASP site into Kentico CMS. The client is very particular about the same Folder and file structure because of the SEO (Search Engine Optimization). The other issue is moving of the content from the data store to the ‘Editable Text’ web part of the pages, which are large in number have to be created dynamically.

  1. Creating folder structure from existing site.

//Getting folder Structure from Existing site

DirectoryInfo parentInfo = new DirectoryInfo(@”Existing site local copy path”);

DirectoryInfo[] childInfo = parentInfo.GetDirectories ();

if (childInfo.Length > 0)


for (int i = 0; i < childInfo.Length; i++)


//***Folder creation***//

CultureInfo culture = CultureInfoProvider.GetCultureInfo(“de-de”);


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


// Creating the folder

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

newNode.DocumentName = childInfo[i].ToString();

newNode.DocumentCulture = “en-us”;





  1. After creating the folder I want create the pages. Since all the pages in the site are similar in look and feel, I created a Page Template, and I created the pages using the below code.

(You need to capture the template id to create pages dynamically).

// Create new instance of the Tree provider

TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

        // Get parent node

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

       string[] parfiles = Directory.GetFiles(@”Existing site local copy path”);

       if (parfiles.Length > 0)


                         for (int f = 0; f < parfiles.Length; f++)


                        string extension;


DataTable dtc “”));

                         if (extensi “.asp”)


                   //Create instance for the new template

                   PageTemplateInfo templateInfo = null;

                   templateInfo = new PageTemplateInfo(true);

                            // Create TestPage under parent folder

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

                           newNode.DocumentName = parfiles[f].ToString();


                           //The page will displayed on site map

newNode.SetValue(“DocumentShowInSiteMap”, true);

                              //The page will not displayed on Navigation

                              newNode.SetValue(“DocumentMenuItemHideInNavigation”, true);

                             newNode.DocumentCulture = “en-us”;


  1. Finally, Moving the content from the data store to the editbale webpart in the pages

//Start accessing web part and adding content

 PageInfo parentpi = PageInfoProvider.GetPageInfo(CMS.CMSHelper.CMSContext.CurrentSiteName, “”, CMS.CMSHelper.CMSContext.CurrentDocumentCulture.CultureCode, “”, newNode.NodeID, false);

 if (dtcontent.Rows.Count > 0)


parentpi.EditableWebParts[“editabletext”] =

(dtcontent.Rows[0][“Content”].ToString() != “”) ?       dtcontent.Rows[0][“Content”].ToString() : “”;




                                        parentpi.EditableWebParts[“editabletext”] = “”;


           // Set DocumentContent value

           newNode.SetValue(“DocumentContent”, parentpi.DocumentContent);

           // Update node


Ray Business Technologies is the pioneer in providing innovative IT Solutions in the areas of Social Media, Enterprise Portals, Magento eCommerce , Joomla cms, kentico cms,eEnterprise Resource Planning, Embedded Systems, Enterprise Portals and Content Management, CRM, and Enterprise Mobility Solutions.


About Author

Thomas Neal was born and raised in the Bronx, New York. He was a bookseller before shifting to publishing where he worked at a literary development company, a creative writing website for millennials, and as a book reviewer of adult and young adult novels. He lives in New York City and is obviously a voracious reader. He has just released his debut novel and working on his second already!

Leave A Reply