I think I should make a correction to the title, it's not really OData -- it's the SDLTridion Content Delivery Webservice, which supports the OData protocol.
There is a bit of confusion about what can be done with it and what can't, so the intention of this post is to (try) clarify somewhat its capabilities.
This post assumes you have OData installed and you have a client configured to read the API (if you don't, then you can start by reading the stuff on SDLTridionWorld). I am using a .NET client, as described in the previous link.
So what can be done with CD Webservice (aka OData)? Well, pretty much everything you can do with the CD API. However, not everything. For example you cannot really do dynamic 'broker' queries. I'll explain some details below.
Let start with the basics... In my client code, I setup the Content Delivery Webservice object using the following construct (again, I refer back to SDLTridionWorld article for creating the proxy classes for this client)
Behind the scene when calling GetCPContentByUri(service, "tcm:1-36"), the proxy calls the following URL:
When executing the call GetPageContentByUrl(service, "/CwaRefImpl/system/style.css"), the following URL is called on the Webservice:
/odata.svc/Pages()?$filter=Url%20eq%20'%2FCwaRefImpl%2Fsystem%2Fstyle.css'&$top=1&$expand=PageContent
I can only speculate what happens here, but this kind of queries are supposed to be supported by OData protocol itself (in fact the Northwind samples from Microsoft do have examples on such queries). Therefore, this is clearly a limitation of Tridion's CD Webservice implementation of OData.
In the code below, I am retrieving items from the ComponentPresentations collection, but querying on linked entity Component's property Title. What I should do is query on properties of ComponentPresentation's.
In order to still have the functionality for retrieving CPs by Component title, I need to rewrite the query. I will retrieve Components, then filter by their Title, then expand ComponentPresentations linked entity and return its PresentationContent property. A bit cumbersome, but it works.
The following query will not fail, but it will return empty (default) object, in other words, null.
The query above should in fact be on service.Components where Component.CustomMeta.KeyName="something", but that's not supported (see first limitation above).
There is a bit of confusion about what can be done with it and what can't, so the intention of this post is to (try) clarify somewhat its capabilities.
This post assumes you have OData installed and you have a client configured to read the API (if you don't, then you can start by reading the stuff on SDLTridionWorld). I am using a .NET client, as described in the previous link.
So what can be done with CD Webservice (aka OData)? Well, pretty much everything you can do with the CD API. However, not everything. For example you cannot really do dynamic 'broker' queries. I'll explain some details below.
Let start with the basics... In my client code, I setup the Content Delivery Webservice object using the following construct (again, I refer back to SDLTridionWorld article for creating the proxy classes for this client)
string CDWS_URL = "http://localhost:8080/odata.svc"; var service = new ContentDeliveryService(new Uri(CDWS_URL));
Accessing Items by ID
The following example reads Component Presentations for a given Component TcmUri, retains the first CP and then returns its content. Notice how easy it looks using Linq.
private string GetCPContentByUri(ContentDeliveryService service, string uri) { string result = string.Empty; TcmUri tcmUri = new TcmUri(uri); var cp = (from x in service.ComponentPresentations where x.PublicationId == tcmUri.PublicationId && x.ComponentId == tcmUri.ItemId select x).FirstOrDefault(); if (cp != null) { result = cp.PresentationContent; } return result; }
Behind the scene when calling GetCPContentByUri(service, "tcm:1-36"), the proxy calls the following URL:
/odata.svc/ComponentPresentations()?$filter=(PublicationId%20eq%201)%20and%20(ComponentId%20eq%2036)&$top=1
Accessing Items by Property
Sample below retrieves a Page by its 'Url' property, then expands the linked entity 'PageContent' and finally returns its Content string. Without expanding it, the page.PageContent would be null.
Since PageContent is in a related entity, I need to expand on this property of Page in order to read the related entity using the same HTTP request. Otherwise, I would have to use more HTTP GETs and performance would not be optimal.
private string GetPageContentByUrl(ContentDeliveryService service, string url) { string result = string.Empty; var page = (from x in service.Pages.Expand("PageContent") where x.Url == url select x).FirstOrDefault(); if (page != null) { result = page.PageContent.Content; } return result; }
Since PageContent is in a related entity, I need to expand on this property of Page in order to read the related entity using the same HTTP request. Otherwise, I would have to use more HTTP GETs and performance would not be optimal.
When executing the call GetPageContentByUrl(service, "/CwaRefImpl/system/style.css"), the following URL is called on the Webservice:
/odata.svc/Pages()?$filter=Url%20eq%20'%2FCwaRefImpl%2Fsystem%2Fstyle.css'&$top=1&$expand=PageContent
Limitation 1 - Querying by Related Item Property
It is only possible to query items by their *own* properties. Attempting to query by another item's property (even if linked to the current items collection) will result in run-time error System.Data.Services.Client.DataServiceClientException
can not execute query, check filter expression
I can only speculate what happens here, but this kind of queries are supposed to be supported by OData protocol itself (in fact the Northwind samples from Microsoft do have examples on such queries). Therefore, this is clearly a limitation of Tridion's CD Webservice implementation of OData.
In the code below, I am retrieving items from the ComponentPresentations collection, but querying on linked entity Component's property Title. What I should do is query on properties of ComponentPresentation's.
private string GetComponentPresentationByComponentTitle(ContentDeliveryService service, string componentTitle) { string result = string.Empty; // Limitation - this won't work... :( // cannot query by properties in referenced entities var cp = (from x in service.ComponentPresentations where x.Component.Title == componentTitle select x).FirstOrDefault(); if (cp != null) { result = cp.PresentationContent; } return result; }
In order to still have the functionality for retrieving CPs by Component title, I need to rewrite the query. I will retrieve Components, then filter by their Title, then expand ComponentPresentations linked entity and return its PresentationContent property. A bit cumbersome, but it works.
private string GetComponentPresentationByComponentTitleFIX(ContentDeliveryService service, string componentTitle) { string result = string.Empty; var component = (from x in service.Components.Expand("ComponentPresentations") where x.Title == componentTitle select x).FirstOrDefault(); if (component != null) { result = component.ComponentPresentations[0].PresentationContent; //check bounds } return result; }
Limitation 2 - Query on Multiple Custom Meta
That's right, you can only query on one Custom Meta at a time (KeyName="State" and StringValue="California"), which makes perfect sense. You cannot use more than one query (KeyName="State" and StringValue="California" and KeyName="Type" and StringValue="Article") because you are in fact querying on the same DB Table (CustomMeta), where KeyName and StringValue are columns. Therefore, there will never be a record that can hold at the same time different values for the same column (e.g. KeyName).
The following query will not fail, but it will return empty (default) object, in other words, null.
private string GetComponentByCustomMeta(ContentDeliveryService service, string key1, string value1, string key2, string value2) { string result = string.Empty; var customMeta = (from x in service.CustomMetas.Expand("Component") where x.KeyName == key1 && x.StringValue == value1 && x.KeyName == key2 && x.StringValue == value2 select x).FirstOrDefault(); if (customMeta != null) { result = customMeta.Component.Title; } return result; }
The query above should in fact be on service.Components where Component.CustomMeta.KeyName="something", but that's not supported (see first limitation above).
Comments
The following code will work:
var cp = (from x in co.ComponentPresentations
.Expand("Component")
.AsEnumerable()
.Where(c => c.Component.Title == componentTitle)
select x).FirstOrDefault();
Not sure, but I think this query works because it pulls every ComponentPresentation from the database and applies the 'Where' clause on the client. Worth to investigate this further... :)
The actual query is this:
var cp = (from x in service.ComponentPresentations.Expand("Component") select x)
.AsEnumerable().Where(x => x.Component.Title == componentTitle).FirstOrDefault();