Adding pages with C#

Add Pages from Code

You can add pages programmatically using C#. In the current version of C1 CMS you can do it by creating raw page objects using the CMS Data API.

We have encapsulated this logic in a more user-friendly function call, available in this PageCreationDemo class.

using System;
using System.Globalization;
using Composite.Data;
using Composite.Data.Types;


namespace Demo
{
    /// <summary>
    /// A demo class that allows you to create pages programmatically
    /// </summary>
    public static class PageCreationDemo
    {
        /// <summary>
        /// Adds a new page to the system and publishes it. This sample expects that 
        /// the Layout Template used has a &lt;rendering:placeholder /&gt; with id="contentplaceholder" 
        /// </summary>
        /// <param name="parentId">ID of the parent page for structural placement. Use Guid.Empty for the top-level page.</param>
        /// <param name="templateId">ID of a template to use</param>
        /// <param name="pageTypeId">ID of a page type to use</param>
        /// <param name="title">Page title</param>
        /// <param name="pageDescription">Page description</param>
        /// <param name="pageCulture">Language (culture) of the page</param>
        /// <param name="menuTitle">String to use in menues. Use an empty string to keep this page out of navigations.</param>
        /// <param name="urlTitle">A single folder name to use in URLs</param>
        /// <param name="pageContentXhtml">Content of the page. This demo only supports single content</param>
        /// <returns></returns>
        public static Guid CreateNewPage(
            Guid parentId,
            Guid pageTypeId,
            Guid templateId,
            string title,
            string pageDescription,
            CultureInfo pageCulture,
            string menuTitle,
            string urlTitle,
            string pageContentXhtml)
        {
            using (var connection = new DataConnection(PublicationScope.Unpublished, pageCulture))
            {
                ////// *** PAGE *** //////

                IPage page = DataConnection.New<IPage>();

                page.Id = Guid.NewGuid();
                page.PageTypeId = pageTypeId;
                page.TemplateId = templateId;
                page.Title = title;
                page.Description = pageDescription;
                page.MenuTitle = menuTitle;
                page.UrlTitle = urlTitle;
                page = page.AddPageAtBottom(parentId);

                ////// *** PLACEHOLDER *** //////

                IPagePlaceholderContent content = DataConnection.New<IPagePlaceholderContent>();

                content.Content = pageContentXhtml;
                content.PlaceHolderId = "contentplaceholder";
                content.PageId = page.Id;

                content = connection.Add(content);

                ////// *** PUBLISHING *** ///////

                page.PublicationStatus = "published";
                connection.Update(page);

                content.PublicationStatus = "published";
                connection.Update(content);
                
                return page.Id;
            }
        }
    }
}

Download the sample code

This function requires information about the page title, content, page type ID, page template ID etc.

Note. In v 5.1 or later, you can use a new extension method page.Add(parentId, pageInsertionPosition). It adds both the page at a given position in the tree and data related to the specified page type (default content for placeholders, page data folders and applications). This makes the code that adds page placeholder content optional.

Show sample code for v 5.1 or later

Testing PageCreationDemo

Here is the sample code that calls the function to create a page:

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Composite.Data;
using Composite.Data.Types;
using Composite.Core.PageTemplates;

using Demo;


/// <summary>
/// Summary description for TestCreatePageDemo
/// </summary>
public class TestCreatePageDemo
{
   public static string CreatePage()
   {
       using (DataConnection connection = new DataConnection())
       {
           Guid pageTypeId = connection.Get<IPageType>().First().Id; // grab some page type id 
           Guid templateId = PageTemplateFacade.GetPageTemplates().First(template => template.IsValid).Id; // grab some template id

           Guid parentId = Guid.Empty; // make new website
           CultureInfo pageCulture = new  CultureInfo("da-DK" ); // we will add a Danish page 
           string content = "<h1>This is content</h1><p>Hello world!</p>" ; // test markup

           Guid addedPageId = PageCreationDemo.CreateNewPage(
               parentId,
               pageTypeId,
               templateId, 
               "The Page Title", 
               "Page description...",    
               pageCulture, 
               "Menu Title", 
               "urltitle", 
               content);  

           return string.Format("A page added with ID {0}", addedPageId);
       }
   }
}

Download the sample code

(For a quick test, you can add it as a C# external method in the CMS Administrative Console.)