To me, OData is simply the Broker API exposed as a web service – at least the most popular functionality of the original Broker API. Â This is useful if your website is not .NET or Java based (PHP for instance), or if the app that needs to pull content across the internet or via AJAX, or for some other reason where database calls to the Broker are restricted. Â In this tutorial I cover getting started with OData and also dive into some of the more advanced query string operations.
A common argument that architects make is that they donâ€™t want to introduce dependencies on third party APIs. Â In this case, I donâ€™t buy it. Â Just because youâ€™re not including a third-party DLL or JAR file as part of your codebase, doesnâ€™t mean you havenâ€™t introduced dependencies. Â You still need code that is specific to binding to the web service and working with its entities (e.g. proxy classes and the usual crap when working with any web service). Â This is simply additional effort that we donâ€™t have to expend by using the native API.
So if your website is running .NET or Java and can connect to the Broker Database, then using the native Broker API is probably a better idea. Â It has been around longer, is a read-to-use object oriented set of classes, and offers advanced caching capabilities via the Tridion Object Cache and the Cache Channel Service. Â Letâ€™s keep the XPM (SiteEdit) integration as separate point of discussion, and Tridion 2013 actually addresses this.
If you are a .NETter, I urge you to put LINQ aside for a minute, or until you understand how to manually construct Tridion OData queries and the kind of result sets they return. Â LINQ will not help you make them more efficient, since it automatically constructs them for you. Â If youâ€™re working with Java or PHP, then youâ€™ll have no choice but to learn the fundamentals anyway. Â So here we go…
Step 1 – Install It
Step 2 – Learn the Basics
The next step is to open up Firefox and type in some queries for your published content. Â Remember, OData is a self-descriptive web service. Â Itâ€™s very simple – just look at it. Â
The place to start is by going to the root:
- On my IIS itâ€™s on port 82 and my URL is this: http://localhost:82/odata.svc/
- On my Tomcat its on port 8080 and my URL is this: http://localhost:8080/cd_webservice/odata.svc
Take any one of the entities, append to the URL and see what comes next. Â For instance:
http://localhost:82/odata.svc/ComponentPresentations returns all the Dynamic Component Presentations (limited to the default max resultset specified in the cd_webservice_config.xml).
Inside the result you will see each of the published DCPs as <entry> nodes. Â Take note of a special property called <link>. Â It provides the relative URLs of the linked entities:
For instance, for each Component Presentation we can make a subsequent web service call to retrieve the linked Component, Template and Pages.
This covers the basics.
Step 3 – Advanced Functionality
It is important to be conscious about the number of web service calls our application is making and the impact to your applicationâ€™s performance. Â Each additional HTTP Request/Response is overhead.
So how do we avoid having to make several calls to drill down into a DCPâ€™s Component, Template or Pages?
The answer is
$expand. Â You can expand on any one given linked entity and keep expanding. Â
Here is a simple example:
To get the Component Template of a given Component Presentation:
Here is another example:
To get all the ComponentPresentations associated with a Custom Meta, you need to get to it from CustomMetas, through the Component: Â CustomMetas–>Component–>ComponentPresentations
So now we can get what we need with less HTTP Requests – sweet!
The next important operation I want to discuss is
$filter. Â With this handy op you can filter by any property of an <entry>. Â The query example above already utilizes it and filters on the â€œStringValueâ€ property of a CustomMetas entry. Â As Mihai pointed out in his post (http://yatb.mitza.net/2012/11/tridion-odata-whats-possible-and-what.html), you cannot easily query across multiple CustomMetas entries (actually you can, but that is a separate article).
There are other handy operations like â€œ$topâ€ and â€œ$orderbyâ€, as well as all kinds of operants like â€œeqâ€, â€œsubstringofâ€ and so on. Â For more operations and how to use them, refer to the OData Query String Operations documentation: Â http://www.odata.org/documentation/uri-conventions#4_Query_String_Options
Publish DCPs with REL
OK, I lied, there are actually 4 important points that I wanted to cover, not 3. Â The 4th one being Render Engine Language (REL). Â It complements OData. Â If your Dynamic Component Templates donâ€™t use REL as the Output Format, you will have a lot more work on your hands. Â You will need to handle each Dynamic Link individually by parsing it out from your rich text fields and by invoking the Linking Web Service API (login required): http://sdllivecontent.sdl.com/LiveContent/content/en-US/SDL_Tridion_2011_SPONE/reference_277A2D7264B04A39870C3FE18EF245BB.
With REL, all links appear resolved in your returned content, easy peasy.
Hopefully you find this tutorial insightful. Â After learning the fundamentals behind ODataâ€™s features, go ahead and LINQ it up, Jaxb, Axis2, whatever. Â Remember, if you need to debug a query, the easiest thing to do is to hit the browser with the URL and look at the response.