Create EPiServer page programmatically with PageTypeBuilder

September 29th, 2011

Here is just a few examples of how to create and modify pages in EPiServer CMS 5/6 when you are using PageTypeBuilder.

Create new page:

?View Code CSHARP
1
2
3
4
5
6
7
8
PageReference targetContainer = PageReference.StartPage;
MyPageType newPage = (MyPageType)DataFactory.Instance.GetDefaultPageData(targetContainer, PageTypeResolver.Instance.GetPageTypeID(typeof(MyPageType)).Value);
 
newPage.PageName = "This is the title";
newPage.MyProperty = "Lorem ipsum";
newPage.Category.Add(categoryId);
 
DataFactory.Instance.Save(newPage, EPiServer.DataAccess.SaveAction.Publish);

Modify existing page:

?View Code CSHARP
1
2
3
4
5
6
MyPageType clone = myPage.CreateWritableClone() as MyPageType;
 
// Modify properties
clone.LatestNewsletterDateTime = DateTime.Now;
 
DataFactory.Instance.Save(clone, SaveAction.Publish | SaveAction.ForceCurrentVersion, AccessLevel.NoAccess);

Leave a Reply