In this tutorial, you will learn how to use the project repository within Episerver to create a new project. Afterward, we will use that API to get a list of all the content defined within that project. Episerver released Episerver projects in V9. An Episerver project is simply a container where content editors can group content changes. One of the driving forces behind Episerver projects was marketing campaigns. Some companies run weekly/monthly marketing campaigns which require the content to be changed frequently. Applying and scheduling these campaigns on a per-page level was tedious. It was impossible for someone to create a scheduled multi-marketing campaign and have confidence that when each campaign was published the entire site looked correct. Episerver projects solved this annoyance. Using Episerver projects within the editor is simple and will not be covered here.
There are instances where you may want to manage projects in code, using the project API. A recent situation where I used the project API was during a content migration project. To be polite the Episerver importer can be a bit flakey at times. We had a situation where we wanted to create translation files for specific content within the campaign. The normal Episerver export wasn't working. Using the project API allowed us to create an export file!
Get A List Of Projects
You can access projects in code using the
ProjectRepository. This can be accessed using constructor injection. The code below uses the service locator pattern on to ease understanding:
List() will return you a list of
Project items. This list will include all the projects that have been set up within the editor. If you know the specific project that you want to access, you can get all its related items using the
ListItems() method, like so:
ListItems() will return a list of
ProjectItem will relate to a page that a content editor added to the project. To get information about each individual item, you can use the standard content repository, like so:
An interesting thing to consider about this list is that you will get pages and blocks returned. This is the reason why I'm typing the items to
IContent rather than
BlockData. To create a new project in code you can use the 'Save()' method passing in a new
Project object, like this:
An empty project would be pretty dull. To add a block or a page into the project, you need to create a 'ProjectItem':
To create a project item. Pass in the project ID - this can be found by hoving over the project within the CMS - and the reference to the item you want to add within the project - typed as an