Property Injection & Action Filters in EPiServer 7 CMS

I spent a bit of time putting in some custom implementation to support Property Injection on to my Action Filters before realizing that EPiServer 7 already provides this functionality for me. Doh.

In order to take advantage all you need to do is set your DI controlled interface to the generic type property of the Injected class. This will lazily set the interface when the property is used…awesome.

public class CustomFilter : ActionFilterAttribute
public Injected Service { get; set; }

Manipulating an EPiServer Page Programmatically

Creating a Page

Here’s how you can create a page in code from a page type you’ve already defined.

First create a new instance of the page type under another page in the form of a link:

var newBlogPost = ContentRepository.GetDefault<BlogPostPage>(parentLink);

Then set whatever properties you wish to the newBlogPost object.

newBlogPost.Subject = "Creating an EPiServer Page Programmactically";
newBlogPost.Content = "Meta";

Finally call the Save method on EPiServer’s content repository.

ContentRepository.Save(newBlogPost, SaveAction.Publish)

Updating a page

The structure to updating a page is thankfully very similar to creating one.

Load the page you wish to update from the Content Repository. 5 is the id of the page.

var contentReference = new ContentReference(5)

Create a writeable clone of the update. This allows you to update it’s properties:

var clone = (BlogPostPage)page.CreateWritableClone();

Update a properties of the cloned page

clone.Subject = "Creating an EPiServer Page Programmactically";

Call the Content Repository to Save the page and public it at the same time:

ContentRepository.Save(clone, SaveAction.Publish);


When creating a new page and / or block you may get the following exception:

The INSERT statement conflicted with the FOREIGN KEY constraint 'FK_tblContent_tblContentType'. The conflict occurred in database ' ', table 'dbo.tblContentType', column 'pkID'.

This commonly gets throw when there are two developers working against the same database but have different versions of the page / block you are trying to save.

EPiServer 7 CMS – Import and Exporting Content between Environments

At Marie Curie we have several environments that sit across our infrastructure. Occasionally we get a requirement to shift content from one environment to another. This could be because we’ve auto generated pages via scheduled job or that QA have created a string of pages and want it replicated in another environment without having to key them all in again. Here are the basic steps required to do this.

Export Data

The EPiServer admin section exposes two tools which allow you to do this, Import Data and Export Data. On your source environment navigate to the Export Data tool.

EPiServerAdmin Import Export Data

Content items, content types even property definitions can be exported. What we are interested in is Export Content Items. Tick this box then hit the lookup button. This will make a pop up window appear with your website’s tree structure. Choose the page you are interested. Hit Select then hit Export.

EPiServerAdmin Import Export Data

The export data screen will show in real time the number of items that have been exported and your browser will automatically down a zip file.

EPiServerAdmin Import Export Data

Import Data

Switch to your target environment and once again head to the admin section but this time click on Import Data. here you can select the zip file you just exported and destination of the content. When you are ready click Begin Import.

EPiServerAdmin Import Export Data

Done! You will see that all the content items have been successfully imported.

EPiServerAdmin Import Export Data

I would not recommend this method as part of a permanent solution. It should be used for more one off circumstances. Remember that the target and source environment should have matching content types and property definitions otherwise you may not get all the data transferred correctly. Enjoy!