Quantcast
Channel: Yet Another Tridion Blog
Viewing all articles
Browse latest Browse all 215

Tridion OData - What's Possible and What Not?

$
0
0
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 = newContentDeliveryService(newUri(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.

privatestringGetCPContentByUri(ContentDeliveryServiceservice, string uri) {
    stringresult = string.Empty;
    TcmUritcmUri = newTcmUri(uri);

    var cp = (from x inservice.ComponentPresentations
              wherex.PublicationId == tcmUri.PublicationId &&
                    x.ComponentId == tcmUri.ItemId
              selectx).FirstOrDefault();

    if (cp != null) {
        result = cp.PresentationContent;
    }

    returnresult;
}

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.

privatestringGetPageContentByUrl(ContentDeliveryServiceservice, string url) {
    stringresult = string.Empty;

    var page = (from x inservice.Pages.Expand("PageContent")
                wherex.Url == url
                selectx).FirstOrDefault();

    if (page != null) {
        result = page.PageContent.Content;
    }

    returnresult;
}

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.

privatestringGetComponentPresentationByComponentTitle(ContentDeliveryServiceservice, string componentTitle) {
    stringresult = string.Empty;

    // Limitation - this won't work... :(
    // cannot query by properties in referenced entities
    var cp = (from x inservice.ComponentPresentations
              wherex.Component.Title == componentTitle
              selectx).FirstOrDefault();

    if (cp != null) {
        result = cp.PresentationContent;
    }

    returnresult;
}

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.

privatestringGetComponentPresentationByComponentTitleFIX(ContentDeliveryServiceservice, string componentTitle) {
    stringresult = string.Empty;

    varcomponent = (from x inservice.Components.Expand("ComponentPresentations")
                     wherex.Title == componentTitle
                     selectx).FirstOrDefault();

    if(component != null) {
        result = component.ComponentPresentations[0].PresentationContent; //check bounds
    }

    returnresult;
}

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.

privatestringGetComponentByCustomMeta(ContentDeliveryServiceservice, string key1, stringvalue1, string key2, stringvalue2) {
    stringresult = string.Empty;

    varcustomMeta = (from x inservice.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;
    }

    returnresult;
}

The query above should in fact be on service.Components where Component.CustomMeta.KeyName="something", but that's not supported (see first limitation above).



Viewing all articles
Browse latest Browse all 215

Latest Images

Trending Articles



Latest Images