Create a page programmatically in EPiServer

Denna artikel har migrerats från en tidigare version av vår webbplats och kan därför avvika i utseende och funktionalitet.

Den här artikeln är inte översatt till svenska och visas därför på engelska istället.


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

Uppskattad lästid : 2 minuter

Gå till avsnitt

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(
    PageReference.StartPage,
    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);