Using OData with Tridion (.NET, C#)

With the release of Tridion 2011, SDL has incorporated OData support into its API's for the display layer via web services. By doing this Tridion allows developers to talk to the content broker and retrieve data for consumption within their own code. This guide will walk you through setting up, and using OData for your first time to connect to Tridion

Open Data Protocol

The Open Data Protocol (OData) is an open protocol for sharing data. It provides a way to break down data silos and increase the shared value of data by creating an ecosystem in which data consumers can interoperate with data producers in a way that is far more powerful than currently possible, enabling more applications to make sense of a broader set of data. Every producer and consumer of data that participates in this ecosystem increases its overall value.

With the release of Tridion 2011, SDL has incorporated OData support into its API's for the display layer via web services.  By doing this Tridion allows developers to talk to the content broker and retrieve data for consumption within their own code.

The following guide will walk you through setting up, and using OData for your first time to connect to Tridion. This guide also assumes that you have an environment already set up; the guides to do that can be found here:

Step 1: Setting up a Tridion development VM

Step 2: Configure oData Web Service

Once these tutorials are complete, you are ready to start testing out the OData API, so let the fun begin!

Getting Ready

First, let's start by looking at a standard .NET web site project. The first thing we need to do is make sure we can talk to the OData web service. If you don't have Visual Studio open still from the previous tutorials, let's open it and load your Delivery website or create a new standard .NET web site.  Your environment should look very similar to this:

Now that Visual Studio is open and looks like the screen above, you need to add OData as a web service. Right click on your project in the Solution Explorer and navigate to 'Add Service Reference'. Your project and application pool must be running in .NET 4.0 for this option to appear.

You should now see the following dialog, plug your content broker OData URL into the URL value, assign it a web reference name (for the purpose of this documentation, let's call it CDS since we'll be looking at the Content Delivery Service mainly), and click Add Reference:

Now that this is complete you should now have access to use the OData web service! Let's start testing it out and see what the capabilities are. The main classes within OData are the following:

-         Binary
o   Files sent via content delivery in multimedia components
-         BinaryVariant
o   Binary defined as different version by user
-         Component
o   Tridion components, added to component presentations and used to build templates
-         ComponentPresentation
o   Tridion component presentations, components combined with their component templates
-         ContentDeliveryService
o   Probably the one you'll use the most, access to your content on your content delivery side
-         CustomMeta
o   Access to Tridion metadata
-         Keyword
o   Hierarchy used within Tridion to categorize content
-         Page
o   Pages, combination of content, templates and component presentations
-         Publication
o   Publications, or where you're publishing your website
-         Schema
o   Tridion schemas, used to define content
-         StructureGroup
o   Groups within the system that store content, pages, keywords, etc.
-         Template
o   Tridion templates, presentation for pages

Using oData with Visual Studio and Linq

Looking at this list we can immediately see the value and a lot of what's available to us. I chose the ContentDeliveryService for the purpose of this tutorial, but have fun exploring the other functions, they are all quite useful. First lets instantiate the content delivery service with the following code, which also requires the URI in the constructor

 Uri uri = new Uri("http://localhost:8080/cd_webservice/odata.svc");
            CDS.ContentDeliveryService cds = new CDS.ContentDeliveryService(uri);

Now that we have the OData service available to us the real fun begins! The OData web service uses Linq to pull back the majority of the data. So let's look at a quick example of how to pull out your Structure Groups.
 
(For more information on using Linq and for popular commands, you can reference the following link: http://msdn.microsoft.com/en-us/library/bb397678.aspx)

 var sgroups = from p in cds.StructureGroups
                                  select p;
 
            foreach (CDS.StructureGroup sg in sgroups)
            {
Response.Write(sg.Title);
            }
 With a small Linq statement above, we can get a complete list of Structure Groups within the system.
 
You can immediately see the value of using several technologies together to make your delivered content accessible in an easy to manipulate API.
 
I'll leave you with a slightly more complex example that joins components based on keywords, please leave some feedback and maybe some links to your own examples in the forums!
 
            var keywordcombine = (from c in cds.Components
             join k in cds.Keywords on c.Keywords equals k.Children
             select new { C = c, K = k }).
            Distinct();
 
 
            foreach (var v in keywordcombine)
            {
                Response.Write(v.C + "::" + v.K);
            }
 
If you're looking for more Linq examples there is a great assortment here:
http://msdn.microsoft.com/en-us/vcsharp/aa336746