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

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



2.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;

              extension = Path.GetExtension(parfiles[f].ToString());

DataTable dtcontent = getUrls(parfiles[f].ToString().Replace(@"D:\1800duilaws\1800duilaws\,   ""1800duilaws.com""));

              if (extension == "".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"";


3.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


- My ASP.NET Application
We use cookies to provide the best possible browsing experience to you. By continuing to use our website, you agree to our Cookie Policy