In the past I already talked a lot about the Content Presenter. It is very powerful to display content in all sorts of way. It can interact with Site Studio elements and it is easy to use by using its wizard.
One of the hidden gems of the content presenter is not the wizard but the plain old input parameters you would expect from a taskflow. The wizard everybody uses to configure the content presenter is just a facade to hide the complexity of those input parameters. In most cases this is sufficient but when you want to do more complex things, you would soon bump into issues by using the wizard.
Suppose that you want to display a list of news items from different categories and display a filter so the user can select their preferred category. There is no way of doing this by using the normal way. You would need to introduce a parameter in the query of the content presenter.
In this example I will explain how it is done. You can download the example application from here.
In order to make this work, you have to add a custom metadata field to UCM: xCategory. This field will be used in our query to filter on the category the user wants.
In the application you can find a news.jspx page which has 3 buttons. One that will display all the news items, one for the sport items and one for the normal news items:
As you can see, these buttons don't have a direct actionListener but they use the setActionListener to invoke a setter from a property of my managed bean. By doing this, we know which items to display and we can build the query.
The query is a CMIS query. CMIS resembles to a normal SQL so the syntax should be familiar if you know SQL. I explained a little bit more about CMIS in another post
The dynamic query will also be build in the NewsBean:
As you can see, depending on the state of the news and sport boolean, we add them to the query.
The next thing is to provide this query to the content presenter.
When you take a look at the bindings of the news.jspx page, you will see that I use following values for the content presenter input parameters:
I'll come back to the tempalteView in a minute...
Also notice that the Refresh attribute of the content presenter is set to ifNeeded. This is required for the taskflow to refresh when you press one of the 3 buttons.
When you run this without specifying a templateView, the content presenter will use the default one. When you are using a CMIS query in the datasource, the content presenter will know that he has to use a multiple items template because the query can return multiple items. This will result that you will get a list of links from all the items returning in the query:
Let's say that all of these items are wiki documents than you want to display the actual content instead of a link. This can be easily done by creating a content presenter template for multiple items that uses the default view for single items:
As you can see, nothing fancy. The most important part is that we include the single item template without specifying a templateView. This is because than it will use the default one being the one that displays the actual content.
When you run this, you will see that the actual content will be displayed:
You also have to make sure that the content presenter template is not hidden in the resource manager. Otherwise it will default back to the regular one with the links so if you have created the content presenter template and assigned it to the taskflow, make sure to check the content presenter templates in runtime to see if its stats is set to shown:
You can also do the exact same at runtime! So let's create a new page at runtime and add an empty content presenter on it:
Now instead of opening the wizard, open the properties by clicking on the wrench in the gray box:
In the data source dropdown you can select Query Expression which will tell the content presenter that the data source field will contain a CMIS query.
Now you can specify whatever query you want in the Data Source. Let's say we want to make a sports page and only display the sport items. We can use following value:
What makes this even more powerful is the fact that you can use Expression Language in the data source field so that query can also be dynamic.:
So as you can see, the content presenter is really powerful. Even more than you might think!