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

Toolkit - Dynamic Content Queries

This post if part of a series about the  File System Toolkit  - a custom content delivery API for SDL Tridion. This post presents the Dynamic Content Query capability. The requirements for the Toolkit API are that it should be able to provide CustomMeta queries, pagination, and sorting -- all on the file system, without the use third party tools (database, search engines, indexers, etc). Therefore I had to implement a simple database engine and indexer -- which is described in more detail in post Writing My Own Database Engine . The querying logic does not make use of cache. This means the query logic is executed every time. When models are requested, the models are however retrieved using the ModelFactory and those are cached. Query Class This is the main class for dynamic content queries. It is the entry point into the execution logic of a query. The class takes as parameter a Criterion (presented below) which triggers the execution of query in all sub-criteria of a Criterio

A DD4T.net Implementation - Custom Binary Publisher

The default way to publish binaries in DD4T is implemented in class DD4T.Templates.Base.Utils.BinaryPublisher and uses method RenderedItem.AddBinary(Component) . This produces binaries that have their TCM URI as suffix in their filename. In my recent project, we had a requirement that binary file names should be clean (without the TCM URI suffix). Therefore, it was time to modify the way DD4T was publishing binaries. The method in charge with publishing binaries is called PublishItem and is defined in class BinaryPublisher . I therefore extended the BinaryPublisher and overrode method PublishItem. public class CustomBinaryPublisher : BinaryPublisher { private Template currentTemplate; private TcmUri structureGroupUri; In its simplest form, method PublishItem just takes the item and passes it to the AddBinary. In order to accomplish the requirement, we must specify a filename while publishing. This is the file name part of the binary path of Component.BinaryConten

Scaling Policies

This post is part of a bigger topic Autoscaling Publishers in AWS . In a previous post we talked about the Auto Scaling Groups , but we didn't go into details on the Scaling Policies. This is the purpose of this blog post. As defined earlier, the Scaling Policies define the rules according to which the group size is increased or decreased. These rules are based on instance metrics (e.g. CPU), CloudWatch custom metrics, or even CloudWatch alarms and their states and values. We defined a Scaling Policy with Steps, called 'increase_group_size', which is triggered first by the CloudWatch Alarm 'Publish_Alarm' defined earlier. Also depending on the size of the monitored CloudWatch custom metric 'Waiting for Publish', the Scaling Policy with Steps can add a difference number of instances to the group. The scaling policy sets the number of instances in group to 1 if there are between 1000 and 2000 items Waiting for Publish in the queue. It also sets the