Filtering in a Sitecore 7 Multilist with Search or Treelist with Search

MumblesThe new “with Search” fields in Sitecore 7 offer a great way for users to select items from a large list of options. One problem is that these new fields have a lot of hidden potential that is not (yet) documented very well.

The Source

It seems that all of the advanced control that a developer has over these fields is hidden in the “Source” property. Rather than spew a lot of words at you, I’ll dive right in to the values and some examples.

Filter (Multilist with Search only)

Expected to be a colon/pipe delimited list of key-value pairs. As of Sitecore 7.0.0 rev. 130810, it doesn’t do anything except enable the advanced filtering options listed below. If this value is omitted or blank, then the advanced filtering rules below are ignored.

Filter=a:b

This does not apply to the Treelist with Search.
UPDATE: As of Sitecore 7.1 rev. 140130, this arbitrary filter is not necessary. Instead, you need to either provide a valid filter (in the form of a lucene query, e.g. “_content:*iceland*”), or omit this parameter.
UPDATE: As of Sitecore 7.2 rev. 140314, Treelist with Search has been deprecated. Going forward, you will need to use Multilist with Search instead.

TemplateFilter

Filters all search results by the given template:

Filter=a:b&TemplateFilter={3569EA2B-F2AD-450F-9707-5D5CEF745C70}

PageSize

Sets the number of items to be displayed on each page of search results (defaults to 10)

Filter=a:b&PageSize=7&TemplateFilter={3569EA2B-F2AD-450F-9707-5D5CEF745C70}

SortField

Sets the field used to sort the results.

Filter=a:b&SortField=ZipCode

Language

Restricts results to only the specified language.

Filter=a:b&Language=en

UPDATE: Mark has pointed out in the comments that Sitecore 7.2 (and maybe others) require the full name of the language here (e.g. “english”), but be warned that the full name of a language can be different based on the backend language. Mark suggests (and I agree) that you should use “&Filter=_language:en” instead.

StartSearchLocation

This is one of the most interesting options. This indicates a parent node, and only results beneath this node will be returned.

StartSearchLocation={DC7AAEA0-73D9-49C6-8847-B607277A2A55}

Even more interesting, is that you can specify a query here!

StartSearchLocation=query:ancestor::*[@@name->'Home']

Important! Any equal signs in the query should be changed to “->” to avoid parsing errors. Sitecore will interpret these as normal “=” when it executes the query.

Closing and a word of caution

I found this most useful in a multi-site install I am working with. Templates are shared between sites, but I only wanted the users to be able to choose items from the site they were editing.

Be warned! The apparent inconsistencies and lack of documentation makes me think that this functionality may change in future releases. Use with caution, and re-test your filters if you upgrade in the future!

10 Responses to “Filtering in a Sitecore 7 Multilist with Search or Treelist with Search”

  1. Mark

    Watch out when using the &Language= param. In Sitecore 7.2 it only accepts full names of the language i.E “english”. We found though that the full name of the languages can vary according to the Sitecore backend language. That’s why it’s better to use &Filter=_language:en instead.

    Reply
  2. piyush

    Hi Patrick,

    Is there way to pick the latest version of the item and default the language to the items language. For example I have an item “ABC” with two version in English and one in Chinese. When i search the treelist with search control on a Chinese item, i get all the above 3 versions with no other information on what is what. Instead i would expect the a query that would return me version 1 of the Chinese item.

    Reply
    • Patrick

      Hmm. That is an interesting question. Unfortunately, the data source is not tied to the item’s language.

      I do not think it is possible to use the language of the item you are editing as part of the filter, and I know that adding different language versions of your template doesn’t allow each language to have different “source” values, so that won’t work either.

      I think the only solution might be to have separate data templates, and explicitly filter by language in your data source definition.

      Reply
  3. Henning Kilset

    Where do you specify these in the source field? After the source starting point, (in my case ‘/sitecore/content/sitename/basedata/category’ with a special delimiter?

    Reply
    • Patrick

      You will need to change your starting point to the item Guid, instead of the path, and use it as the StartSearchLocation.

      Let’s assume your “category” item has the id “{FEC8EFEE-3BA2-40B7-95AC-32D59AEC3F63}”.

      In the Source property, instead of this:
      /sitecore/content/sitename/basedata/category

      It would be something like this:
      Filter=a:b&TemplateFilter={3569EA2B-F2AD-450F-9707-5D5CEF745C70}&StartSearchLocation={FEC8EFEE-3BA2-40B7-95AC-32D59AEC3F63}

      Reply
      • Patrick

        I just tried it, and this also works in Sitecore 7.1 (in case you don’t want to use the Guid):

        Filter=a:b&TemplateFilter={3569EA2B-F2AD-450F-9707-5D5CEF745C70}&StartSearchLocation=query:/sitecore/content/sitename/basedata/category

        Reply

Leave a Reply