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

// 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”; newNode.Insert(parentNode); } } }

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

//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(); newNode.SetDefaultPageTemplateID(templateInfo.PageTemplateId);

//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”; newNode.Insert(parentNode);

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() : “”; } Else { parentpi.EditableWebParts[“editabletext”] = “”; } // Set DocumentContent value newNode.SetValue(“DocumentContent”, parentpi.DocumentContent);

// Update node newNode.Update();

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.