Create a page programmatically in EPiServer

Code sample demonstrating how to create new EPiServer pages programmatically using the DataFactory class.

  • Ted Nyberg
  • 21 May 2010
  • 0

Create a new page in code

The EPiServer API makes it easy to create new pages through code through the DataFactory class by calling the GetDefaultPageData method and passing a reference to the parent page and the page type ID for the new page.

Note that this method is executed using the current user, so if no user is logged in this method will throw a security exception unless you specify the AccessLevel parameter when calling the Save method:

// Create a new page
var newPage = DataFactory.Instance.GetDefaultPageData(
    PageType.Load("Standard page").ID);
// Set the name and URL segment (page name in address)
newPage.PageName = "My new standard page";
// Set the URL segment (page name in address)
newPage.URLSegment = UrlSegment.CreateUrlSegment(newPage);
// Publish the page regardless of current user's permissions
DataFactory.Instance.Save(newPage, SaveAction.Publish , AccessLevel.NoAccess);

Create a new page without publishing it

You can use different overloads of the Save method to save a page without publishing it. The following code would save the page if the current user has at least Read access for it:

// Save page if user has at least Read access
DataFactory.Instance.Save(newPage, SaveAction.Save, AccessLevel.Read);