Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Loading...
Area: Optimizely CMS
ARCHIVED This content is retired and no longer maintained. See the latest version here.

Recommended reading 

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:
    PageReference parent = PageReference.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 name of the page type to use:
    IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
    IContentTypeRepository contentTypeRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentTypeRepository>();
    
    PageData myPage = contentRepository.GetDefault<PageData>(parent, contentTypeRepository.Load("Standard page").ID);
    • You also can use a strongly typed model. Given that you have created a model that looks like this:
      [ContentType]
      public class StandardPage : PageData
      {
          public virtual string MainBody { get; set; }
      }
    • You can achieve the same by using the following generic overload of GetDefaultPageData:
      StandardPage standardPage = 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 (which also defines the URL segment):

    Note: You can use other overloads of the GetDefaultPageData method.

    myPage.PageName = "My new page";
    myPage.URLSegment = EPiServer.Web.UrlSegment.CreateUrlSegment(myPage);

    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:

    standardPage.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);
Do you find this information helpful? Please log in to provide feedback.

Last updated: Sep 21, 2015

Recommended reading