Indexing a Multilist Field in Sitecore 7

When it came time to index a multilist field in Sitecore 7, I didn’t exactly know where to start. Although, I had successfully been able to add a link/droplink field into the index by using the following snippet:

[IndexField("Link Field Name")]
public ID LinkFieldName{ get; set; }

While the code above allows you to index a single item by ID, I needed to index multiple items by ID. I finally got around to double checking the “Developer’s Guide to Item Buckets and Search” documentation on Sitecore SDN and it gave me the clue I needed. IEnumerable is included in the list of automatic type conversions when mapping Sitecore fields to the index. So with that info I could do the following:

[IndexField("Multilist Field Name")]
public IEnumerable<ID> MultilistFieldName { get; set; }

I knew I was on the right track but I was also having problems figuring out how to grab each ID individually in order to use it in a search statement. After some testing I found that the “.Contains()” method does exactly as needed. It allowed me to check if a specific ID is contained in the list of IDs from the multilist field.

using (var context = ContentSearchManager.GetIndex("sitecore_web_index").CreateSearchContext())
{
    var query = context.GetQueryable<SearchResultItem>()
        .Where(
            i => i.MultilistFieldName.Contains(ID.Parse("{00000000-0000-0000-0000-000000000000}"))
            );
}

There we go! Pretty easy once you figure it all out. There is always something new to learn with Sitecore 7.

Posted in Sitecore
7 comments on “Indexing a Multilist Field in Sitecore 7
  1. Matthew says:

    Strange. IEnumerable is chucking out a null value for me. However, List works. Any idea what gives? Did you have to modify any config files?

    Thanks for the article by the way!

  2. Hmm that is strange. I wonder if anything has changed in the most recent version of 7.0 or the 7.1 release. This was done using Sitecore 7.0 rev. 130918.

    Do you know if anything is getting indexed for that field at all when you use IEnumerable? I’ve been using tools like Luke to view what’s in the index.

  3. For future visitors – Matt’s question about this was answered on stackoverflow. Apparently the issue is with the SOLR provider. The functionality described in this post should work fine with Lucene.

    http://stackoverflow.com/questions/20638285/sitecore-7-linq-poco-classes-how-to-fetch-data-from-treelist

  4. scermat says:

    Yep that was it. We managed to figure it out after spending quite a while debugging things. The SOLR configuration is provided by Sitecore themselves so it was a bit of a head scratcher especially as the documentation seems to make no mention of any changes. Hopefully this may help someone else who comes across the issue.

    Your blog post did help point us in the right direction though, so thanks for that.

  5. Stalin says:

    What would be the field type for such multi-list field in the Sitecore.ContentSearch.Lucene.DefaultIndexConfiguration.config file ?

    ie,

  6. Stalin says:

    <field fieldName=”..” storageType=”YES” indexType=”TOKENIZED” type=” .. ???.. ” …

  7. It looks like it is of type “System.String”. It’s under the “fieldTypes” node.

    <fieldType fieldTypeName="multilist" storageType="NO" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" …

    Is this what you're looking for?

Leave a comment