Using the SDL Tridion 2011 oData Content Delivery Service to Display Dynamic Content

In a previous blog post I covered how to import content into SDL Tridion using the new WCF Service in 2011. In this post I am going to cover how you can use that content, publish it to the Broker database and then display it on your website.

The first step is to create a Dynamic Component Presentation to publish to the Broker. In this scenario I am going to create a Component Template that publishes out XML, which will give greater flexibility in querying and displaying this information on the website. The CT is named "Product Price XML":

Product Price XML Component Template

Source Code:

[gist id=3808097]

Note: There was a bug in the initial Tridion 2011 that caused the CDATA tag to not output correctly but this has now been fixed in the 2011 Hotfix rollup and also in Service Pack 1 - so please ensure that one of these are applied.

As you can only query Component Presentation content through the Content Delivery Service if the content is published to the database, the cd_storage_conf.xml file needs to be configured to publish XML to the database:

[gist id=3808111]

You can now publish the pricing components and they will appear in the database.

Now that this content is published to the Broker Database we need to set up the oData Content Delivery Service so we can query the content.

In my previous post, I have covered how to install the Content Delivery Service on your presentation server, if your deployer has already been set up and you’re using a Microsoft setup.

Once the Content Delivery Service is installed, you now need to add a reference to the oData service in your website:
New Service Reference

The next step is to create a Class that we can use to Deserialize our XML against, so we can query the object using Strongly Typed attributes. For this, I am going to re-use the same structure of the ProductPrice class that was created in my previous blog post:

[gist id=3808100]

We can now create a central utility method that queries the Broker and then returns a list of our strongly typed Product Price Objects. Let’s place it in a ProductPriceUtilities Class:

[gist id=3808103]

This now opens up many avenues to display this data in any format we require. You can easily write a query using LINQ to return a single value:

[gist id=3808105]

You can create your own custom search against the objects etc, there are lots of possibilities to explore.

A few things to note:
If you start to have lots of data stored in the database, you need to make sure that your Tridion caching is set up correctly and maybe even implement your own caching model. We sometimes use our own caching mechanism, which has a file dependency that when the dynamic content is published then, this dependent file is also published, which invalidates the cache and forces it to refresh.

Any questions?

If you need more information or have any questions just get in touch and we'd be happy to answer them for you.