In this tutorial, you will learn how to use the project repository within Episerver to create a new project and then use projects API to get a list of all the content defined with the project.
Episerver releases the 'projects' around version 9. A project is simply a container where content editors can group content changes. One of the driving forces behind projects of creating content was marketing campaigns. Some companies run weekly/monthly marketing campaigns which require the content to be changed frequently. Doing this 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.
Using projects through the editor is simple. There are instances where you may want to manage projects in code. Luckily, this is possible with the correct using the correct API. An example where I have needed to access a project in code was around content import/export. 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. When with 'related options' ticked was selected the export file included more items that we wanted to. The solution to creating a custom exported using the projects API. Sound interesting, let's get to code.
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 service locator mainly for ease of 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 project that you want to access, you can get all the items related to that project using the 'ListItems()' method, like so:
ListItems() will return a list of 'ProjectItems'. A project will contain a list of content that the content editor has created inside the project. To get information about each 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 'PageData' or 'BlockData'.
To create a new project in code you can use the Save() method passing in a new Project, like so:
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 'IConten't reference.