4

I need to build a Page with a list of Components, these Components have Custom Meta defined. In order to build a filter capability, it will be great if I can fetch the list of Meta Keys and list of Meta values.

Is it possible to do it using the SDL Tridion Broker API?

migrated from stackoverflow.com Sep 11 '13 at 0:05

This question came from our site for professional and enthusiast programmers.

  • Hi Jerry and welcome to Tridion Stack Exchange! Feel free to add a tag for SDL Tridion version along with details on what you've tried. – Alvin Reyes Sep 11 '13 at 2:40
  • Are you using Java or .Net? – Raimond Sep 11 '13 at 7:38
5

introduced in version 2009, there is a new Query API for the Broker, since you haven't indicated what version you are asking your question on yet, I'll assume it's 2009, 2011 or 2013.

Items can be retrieved from the Broker by querying on various characteristics:

  • System metadata (item type, title, publish date etc.)
  • Custom metadata (anything from the metadata tab of the Component)
  • Linked Keywords

For example:

string[] keywords = {"Audi"};
CategorizationCriteria c1 = new CategorizationCriteria(3, "Cars", keywords);
Query query = new Query(c1);
string[] results = query.ExecuteQuery();
foreach (string tcmuri in results)
{
    Response.Write("<li>" + tcmuri + "</li>");
}

So this shows that a Query contains one (set of) Criteria, and optionally Criteria can have child Criteria. The Query returns you an array of strings, containing TCM URIs. These URIs you can use to load the actual content of the Component Presentation.

You can find all details in the API guides, available for download from the online documenation (requires login). Alvin also wrote a nice blog on the Custom Meta Criteria.

What you are asking for is to retrieve a list of all Meta keys and values, to base your filter on. There isn't really such a method, the idea behind the Query mechanism is that you know your Schema of the Published Content, and based on that, you already know what Meta keys are available.

The meta values is a different story, when these are Keywords, then they are known (as Taxonomy values), but if they are dates or text values, then they are just that, values. There are no indexes on the values themselves, nor is there a full text index or search capability on the content available.

One thing you can do of course is just fetch all Components with a given Template, and then get the metadata from that. Not sure if it's really what you are looking for but at least you get the idea of the possibilities:

string templateUri = "tcm:3-4-32";
ComponentPresentationFactory cpf = new ComponentPresentationFactory(3);
ComponentMetaFactory cmf = new ComponentMetaFactory(3);
Criteria criteria = new ItemTypeCriteria(16);
Query query = new Query(criteria);
foreach (string uri in query.ExecuteQuery())
{
    ComponentPresentation cp = cpf.GetComponentPresentation(uri, templateUri);
    // check if component presentation is published to the broker
    if (cp != null)
    {
        IComponentMeta meta = cmf.GetMeta(uri);
        CustomMeta customMeta = meta.CustomMeta;
        foreach (DictionaryEntry de in customMeta.NameValues)
        {
            Response.Write(string.Format("{0}: {1} ({2})<br/>", 
                           de.Key, 
                           ((NameValuePair) de.Value).Value, 
                           ((NameValuePair) de.Value).Name));
        }
    }
}
  • First of all, thank you very much for the answers. And I think I am getting your point here. However, it is very common to build an application which support drop down for filters. For example, if I have a schema type PRODUCT. I created a bunch of records. For each records, I provide values for meta_key product_type and the meta_key_value can be electronic/toys/baby. I think it is fair to build a drop down with all the available product_type values. – Jerry Yan Sep 11 '13 at 22:57
  • I get your point and a bit depending on where you want to build your filters around your request is not impossible. If you only want to filter on keywords you can do that with the taxonomy data, else you will have to base the filter on the results you get from the query, so rather than upfront create a filter, get a list first and base the filter on what is in the list. – Bart Koopman Sep 12 '13 at 8:03
  • 1
    One basic approach with Tridion is to publish the list of keys or values independent of the custom meta (CM-side template publishing some XML "content data" using TOM.NET). You might go so far as to filter what's used in the content manager, but it might be better to rely on what's actually published. – Alvin Reyes Sep 12 '13 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.