Create a page programmatically
1 min
Programmatic page creation lets you generate content from code, for example, during imports, migrations, or automated workflows. Use IContentRepository to create, configure, and publish pages.
- Decide where to publish the page in the site structure by specifying a ContentReference object that points to the desired parent page, such as the start page: var parent = ContentReference.StartPage;
- Create an empty page with default values according to the property settings of its page type. Specify the parent under which to create the page and the page type:
IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
PageData myPage = contentRepository.GetDefault<StandardPage>(parent);- After you create an empty page under the specified parent page, specify page property values before you publish the page, as shown in the following example: myPage.PageName = "My new page"; The following example shows how to define the user-defined property MainBody: myPage.MainBody = "<p>This is produced programmatically.</p>"; The following example shows how to assign a content property when a typed model is not known: myPage.Property["MainBody"].Value = "<p>This is produced programmatically.</p>";
- Publish the newly created page by calling the Save method on IContentRepository: contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish);
📘 Note The Save method requires that the current user has permissions to publish a page in this location. If the current user is anonymous and the page still needs to be published programmatically, use the overload that bypasses access checks: contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);