Skip to main content

Tridion OData - What's Possible and What Not?

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)

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.

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

Anonymous said…
Hi Mihai,

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... :)
Mihai Cădariu said…
You are right, Albert. That query does work, but it first retrieves ALL Component Presentations, then applies the Where clause. This obviously will kill a production server :) The called URL on the WS is /odata.svc/ComponentPresentations()?$expand=Component

The actual query is this:

var cp = (from x in service.ComponentPresentations.Expand("Component") select x)
.AsEnumerable().Where(x => x.Component.Title == componentTitle).FirstOrDefault();

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as...

I Have Gone Dark

Maybe it's the Holidays, but my mood has gone pretty dark. That is, regarding the look and feel of my computer and Tridion CME, of course. What I did was to dim the lights on the operating system, so I installed Placebo themes for Windows 7 . I went for the Ashtray look -- great name :) My VM looks now like this: But, once you change the theme on Windows, you should 'match' the theme of your applications. Some skin easily, some not. The Office suite has an in-built scheme, which can be set to Black , but it doesn't actually dim the ribbon tool bars -- it looks quite weird. Yahoo Messenger is skinnable, but you can't change the big white panels where you actually 'chat'. Skype is not skinnable at all. For Chrome, there are plenty of grey themes. Now i'm using Pro Grey . But then I got into changing the theme of websites. While very few offer skinnable interfaces (as GMail does), I had to find a way to darken the websites... Enter Stylish -- a pl...

REL Standard Tag Library

The RSTL is a library of REL tags providing standard functionality such as iterating collections, conditionals, imports, assignments, XML XSLT transformations, formatting dates, etc. RSTL distributable is available on my Google Code page under  REL Standard Tag Library . Always use the latest JAR . This post describes each RSTL tag in the library explaining its functionality, attributes and providing examples. For understanding the way expressions are evaluated, please read my post about the  Expression Language used by REL Standard Tag Library . <c:choose> / <c:when> / <c:otherwise> Syntax:     <c:choose>         <c:when test="expr1">             Do something         </c:when>         <c:when test="expr2">             Do something else         </c:when...