In this tutorial, you will learn how to create a page within Episerver CMS using the power of code alone! Creating a new page within Episerver is very easy using the IContentRepository
. in this tutorial, you will learn everything you need to master this API. if this sounds good to you, read on 🔥🔥🔥
IContentRepository
If you try to create a page within the CMS with the same name as an existing page you will encounter an error 😞. This is why when creating pages in code, your first task should be a check to make sure this situation doesn't occur. For this example, I'm going to create all my pages under the StartPage
:
If you do not do this check, you could get an error and it can upset your user flow, so I recommend you always make this check as well. After you have confirmed its safe to create a page, its time to start adding some content to the page:
Creating a page within the CMS is not as simple as new'ing up a new object based on the page-type definition class. Instead, you will need to use the GetDefault()<T>
method in IContentRepository
. T
will be the type of page that you would like to create. The only parameter that you will need to supply to GetDefault<T>()
is the location within the Episerver page where you want the page to be added.
Adding page content is simply a case of assigning values to the properties defined on the object. When you are happy that the item contains all the content you want to add within the CMS, you can persist that data back into the CMS using the Save()
method found on IContentRepository
.
That's all the code you need to write in order to create content within Episerver CMS. UsingGetDefault<T>()
can be a bit counterintuitive, however, it is an easy API to master. Happy Coding 🤘