There are numerous scenarios when you need to create a page in code rather than the editor, say content migration from an old CMS into EPiserver. Adding pages in Episerver is, luckily, very easy using the IContentRepository. The first thing you need to do is check that a page with the same name doesn't exist. For this example, I'm going to create all my pages under the StartPage but you can add them anywhere you want.
If you do not do this then you will get an error when you save so it's worth checking. Next up is setting up your properties:
The first thing we do is create a default empty page using the GetDefault method in IContentRepository. We simply tell Episerver the type of page we want to create and where we want to create it.
Assign values to the properties we are interested in. Lastly, we pass the page into IContentRepository save method.
Create a default type specifying the parent of where you want it to be created, you fill in the properties you care about and then you save it back to the database.
Remember to check if a page with the same name already exists.. otherwise, you will start to get some unexpected exceptions