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.
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:
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.