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

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven