Wednesday, February 22, 2012

Upgrading SearchFilter Queries from 5.X to 2011 SP1


In this post I will not explain very basic concepts about Tridion Content Delivery, I will assume that you are familiar with it and that you wrote code using the Query and/or SearchFilter class included in one of the following namespaces:

·         Tridion.ContentDelivery.Broker (5.X)
·         Tridion.ContentDelivery.DynamicContent.Filters (20XX)


With the release of Tridion 2009 Tridion.ContentDelivery.Broker namespace was separated into two:

·         Tridion.ContentDelivery.DynamicContent.Filters, this namespace is obsolete since 2009. It was replaced with the new Query namespace, see below.
·         Tridion.ContentDelivery.DynamicContent.Query, this namespace brings the opportunity to make more complex queries and improves the performance. You can read more about it here and if you want to learn more you can always get SDL WCM PS to train you, in which case we might get to know each other J

Since 2009 it is not recommended to use the Filter namespace because is obsolete, however it will be available for backwards compatibility until the next Tridion release. I would not recommend you using it at all, instead you should rewrite all your code with the Query API.

That said, in case you have some code written for 5.X in which you make use of either object –SearchFilter or Query – I will show how to make this code work in 2011 SP1, I haven’t tried with 2009 but there should not be very different.

You have to follow three steps to get this code to work

1.       Update the namespace in the web.config
2.       Update the cd_storage.conf.xml
3.       Update your code

Update the namespace in the web.config

If you have any ComponentLinks, BinaryLinks, PageLinks or ComponentPresentationLinks you will need to update the web.config as follows.

Tridion 5.X controls:

<add tagPrefix=”tridion” namespace=”Tridion.ContentDelivery.WebControls” assembly=”Tridion.ContentDelivery.WebControls” />

Tridion 20XX controls:

<add tagPrefix=”tridion” namespace=”Tridion.ContentDelivery.Web.UI” assembly=”Tridion.ContentDelivery” />

Update the cd_storage.conf.xml

Add the search filter as follows:

<Storages>
<SearchFilter Name=”SearchFilter” class=”com.tridion.broker.components.meta.OracleSearchFilterHome” defaultStorageId=”defaultdb”/>
</Storages>

Add the query and search filter items as follows:



<ItemTypes …>
<Item typeMapping="Query" storageId="defaultdb"/>
<Item typeMapping="SearchFilter" storageId="defaultdb"/>
</ItemTypes>

Update your code

There are some differences between the classes SearchFilter and Query in versions 20XX and 5.X. Below you can see the results of comparing working code between of these two versions



Below is the working code for 2011 SP1 in case you want to compare it with yours.

            string pubUri = string.Format("tcm:0-{0}-1", _publication_id);
            SearchFilter filter = new SearchFilter(pubUri);
            PageMetaFactory pmf = new PageMetaFactory(int.Parse(_publication_id));
            IPageMeta pm = null;
            string currentExecPath = "";
            currentExecPath = Request.CurrentExecutionFilePath;

            if (!string.IsNullOrEmpty(currentExecPath))
            {                   
                pm = pmf.GetMetaByUrl(pubUri, currentExecPath);
            }
            ComponentPresentationFactory factory = new ComponentPresentationFactory(pubUri);
            Query query = new Query();
            try
            {              
                if (!currentCat.Equals(_all_label))
                {
                    query.AddOperator(QueryOperator.And);
                    query.AddCriteria("categorization", "=", "Document Category/" + currentCat);
                    query.AddCustomMetaQuery("KEY_DATE_VALUE >= to_date('01/01/" + currentTab + "','MM/DD/YYYY') AND KEY_DATE_VALUE <= to_date('12/31/" + currentTab + "','MM/DD/YYYY')");     
                    query.AddOperator(QueryOperator.And);
                    query.AddCriteria("categorization", "=", "Website Section/" + _section);
                }
            }           
            try
            {
                StringCollection results = new StringCollection();
                results.AddRange(filter.Match(query, new Sorting("title=desc"), 65535));                    
                StringEnumerator enumerator = results.GetEnumerator();
                ItemsCount = results.Count;                

                ComponentPresentation ps = null;
                string component = string.Empty;
                string CTUri = string.Format("tcm:{0}-{1}-32", _publication_id, _binder_dynamic_template_id);
                string tmp = "";
                if (PagesCount == 1)
                {
                    while (enumerator.MoveNext())
                    {
                        component = enumerator.Current;

                        if (!string.IsNullOrEmpty(component))
                        {
                            ps = factory.GetComponentPresentation(component, CTUri);
                            tmp = ps.Content;


No comments:

Post a Comment