ASP.NET, CMS, EpiServer

Programatically Creating and Updating Pages and Blocks in Episerver

If you want to migrate the data from old cms to Episerver it is very useful to create the Pages from

code rather than Editor with the help of IContentRepository API.

Programmatically creating and saving a new page:

Generate a new content item of type NewsPage using IContentRepository, setting its parent as StartPage:

IContentRepository repo = ServiceLocator.Current.GetInstance<IContentRepository>();

NewsPage newsPage = repo.GetDefault<NewsPage>(parentLink: PageReference.StartPage);

Set required page properties:

newsPage.Name = “Today’s news”;

newsPage.MainBody = new XhtmlString(“<p>This is News Page</p>”);

Save the page with appropriate save action and access level:

ContentReference newPagesRef = repo.Save(newsPage, EPiServer.DataAccess.SaveAction.Publish, EpiServer.Security.AccessLevel.NoAccess);

AccessLevel enum :

When calling IContentRepository.Save method, you can pass an AccessLevel. This is the minimallevel that the current user (i.e. anonymous or logged in visitor or editor) must have in order for the method to succeed. If the current user does not have that minimal level then the Save method throws an exception. Therefore, if you pass the NoAccess value, you are allowing everyuser to successfully call the method.

SaveAction enum:

One of the improvements in CMS 10 is to the IContentRepositorySave API and its SaveAction enum values:

• CheckIn – Checks in a version indicating that it is ready to be published

• CheckOut – Checks out a version to indicate that it is being worked on. (New in CMS 10)

• RequestApproval– Indicate that the version is ready for an approval review.

• Reject – Rejects a version. This is normally done after a review has been done.

• Publish – Publishes a version. The currently published version will automatically transition to a previously published state. • Schedule – Used to schedule a version for automatic publishing at a later date. (New in CMS 10)

• Save – deprecated (it won’t appear in IntelliSense but it would compile)

Content versions and states

A content item that supports different statuses implements IVersionable. The interface contains a property Status that specifies the current status of the content version. A content version can have one of the following different statuses:

• NotCreated

• CheckedIn, CheckedOut

• AwaitingApproval, Rejected

• DelayedPublished, Published, PreviouslyPublished

Programmatically updating an existing page

Content is read-only when retrieved through Getmethod(s) so:

1. Call CreateWritableClone method of any IReadOnlyobject to be able to make changes.

2. Set required page properties.

3. Save the page with an appropriate save action and access level depending on scenario.

ContentReference pageLink = new ContentReference(PageId);

var repo = ServiceLocator.Current.GetInstance<IContentRepository>();

NewsPage newsPage = repo.Get<PageData>(pageLink).CreateWritebleClone() as NewsPage;

if (newsPage != null)


newsPage.MainBody = new XhtmlString(“<p>This was updated programmatically.</p>”);

repo.Save(newsPage, EPiServer.DataAccess.SaveAction.CheckIEPiServer.Security.AccessLevel.Edit);


Programmatically creating new shared blocks

Creating a shared block is similar to creating a page, except BlockData-derived classes do not implement IContent so you must cast the block instance to IContent before you can set the Name property or call the Savemethod

ContentReference forAllSites = ContentReference.GlobalBlockFolder;

var repo = ServiceLocator.Current.GetInstance<IContentRepository>();

var editorial = repo.GetDefault<EditorialBlock>(parentLink: forAllSites);

editorial.MainBody = new XhtmlString(“<p>Hello World!</p>”);

var content = editorial as IContent; content.Name = “MyNewSharedBlock”;

ContentReference newBlocksRef = repo.Save(content, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

Programmatically updating existing shared blocks

Updating a shared block is similar, except:

• We would only need to cast to IContent if we need to change the Name property.

• This example uses a casting expression instead of as keyword when we call Save because that method requires an object that implement IContent.

ContentReference blockLink = new ContentReference(blockid);

var repo = ServiceLocator.Current.GetInstance<IContentRepository>();

EditorialBlock editorial = repo.Get<BlockData>(blockLink).CreateWritableClone() as EditorialBlock;

editorial.MainBody = new XhtmlString(“<p>Hello again!</p>”);




About The Author

Leave a Reply