Last updated: Oct 26 2016

Area: Episerver CMS Applies to versions: 10 and higher
Other versions:

Creating a page programmatically

The following examples show how to create a page programmatically in Episerver CMS and set the MainBody property.

  1. Decide where you want your new page published in the structure of the website by specifying a PageReference object pointing to the desired parent page, such as the start page as in the following code:
    var parent = ContentReference.StartPage;
  2. Create a new empty page with default values, according to the property settings of its page type, by specifying the parent under which to create the page and by specifying the page type:
    IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
    PageData myPage = contentRepository.GetDefault<StandardPage>(parent);
  3. After you created an empty page under the specified parent page programmatically, specify page property values before you publish the page, as shown in the following example:
    myPage.PageName = "My new page";

    The code example below shows how to define the user defined property MainBody:

    myPage.Property["MainBody"].Value = "<p>This is produced programmatically.</p>";

    The following case achieves the same with a strongly typed model:

    myPage.MainBody = "<p>This is produced programmatically.</p>";
  4. Publish the newly created page by calling the Save method of the DataFactory class:
    contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish);

    Note: The method call requires that the current user has the proper permissions to publish a page in this location. This causes a problem if the current user is an anonymous user and you still want the page to be published programmatically. You can use another overload of the Save method to permit the publishing, even if the current user does not have the necessary permissions:

    contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);


Hi there

It seems that as of Epi v10, the UrlSegment class is in an "Internal" namespace and it no longer exposes the "CreateUrlSegment" method. So the example of code from above:

myPage.URLSegment = EPiServer.Web.UrlSegment.CreateUrlSegment(myPage);

Is no longer valid as far as I can tell..

So could anyone let me know if there's a way to do this in v10? A different API?


Ah huh, found it - the method name had changed :) Should now use:

myPage.URLSegment = EPiServer.Web.Internal.UrlSegment.GetUrlFriendlySegment(pageNameStringHere);


Actually, further to this, the method is actually obsolete now (confusing since it changed name and then was marked with obsolete). But now should be doing the following:

var urlSegmentGenerator = ServiceLocator.Current.GetInstance<IUrlSegmentGenerator>();
defaultPageData.URLSegment = urlSegmentGenerator.Create(pageNameStringHere);

Hope this helps someone confused like I was :)


Thanks Matthew, I actually removed that part since the URL segment is created for you on publish there is usually no point in doing that yourself.