0

I'm trying to set up an item to allow a content editor to easily select a group of items in a subtree.

I can use a droptree that points to a root item and then a Multilist to select template types to use but I ran into the field type Query Builder. That seems like it can do what I want and more (potentially omit specific items that would be a match using the droptree/multilist method which I cannot do for the other option but would like to be able to do).

I'm having trouble using this though. I didn't see any fields that made sense to cast a field with the type Query Builder to but tried to do a few anyway which didn't seem to work out.

I also tried using Sitecore.Data.Database.SelectItems but got errors about the string being invalid (sample raw values: +location:{ID};template:{ID}).

If anyone has any input on using this field that would be appreciated. I know that you can use the query builder while setting the datasource of a rendering so I will look into how that is set (ItemResolver?).

Based on Filtering results when field is empty with query builder fields it looks like I will need to get a search context and pass this query to that.

3 Answers 3

3

Having the need to use the QueryBuilder fieldtype aswell i was digging around with ILspy too see how i could actually execute the query generated by the field.

In sitecore 8.2 (not sure if this is valid for newer or older versions), there is a helper method defined in the namespace Sitecore.ContentSearch.Utilities

You can do something like this to execute the query:

var index = ContentSearchManager.GetIndex("sitecore_web_index");

using (var context = index.CreateSearchContext())         
{            
    var solrSearchContext = context as Sitecore.ContentSearch.SolrProvider.SolrSearchContext;

            var searchQuery = Sitecore.ContentSearch.Utilities.SearchStringModel.ExtractSearchQuery( sourceItem["SearchQuery"]);
            var results = Sitecore.ContentSearch.Utilities.LinqHelper.CreateQuery<SearchResultItem>(context, searchQuery);           
}

the result of the query will yield the Solr results and you can use this as you please.

1
  • This looks like what I wanted at the time. Nice find!
    – Teeknow
    Commented Jul 10, 2018 at 13:53
2

I tend to find a treelist control with support for multiple roots works well as described by Kam here: https://kamsar.net/index.php/2015/05/A-Multiple-Root-Treelist-Field/

Then you can use all the filters available to filter out templates and so on to only show the items you want:

https://getfishtank.ca/blog/treelist-data-source-hidden-functionality

e.g:

ExcludeTemplatesForSelection, ExcludeItemsForDisplay or ExcludeTemplatesForDisplay

This might work as an approach instead of using the Query Builder type.

6
  • Would a content editor be able to exclude a specific item of the correct template type with this method?
    – Teeknow
    Commented Apr 25, 2017 at 14:29
  • I'm not sure what you mean by this. You want content editors to be able to filter out content when filling in an items details in Sitecore? Isn't this more about ensuring you are setting this field up correctly so the fields list doesn't show items it shouldn't to content editors ? If so then just using the exclude and include options to filter in/out items for a certain template should do the job. Commented Apr 26, 2017 at 14:52
  • I think you're misinterpreting what I'm doing for now vs what I want to do. I want the content editors to have the flexibility in this field to write something that says "choose everything but this" instead of having to manually select everything but the item that they want to exclude.
    – Teeknow
    Commented Apr 26, 2017 at 17:42
  • Ok so to clarify let me run through an example. If the list control lists a bunch of items - lets say these are based on a 'product' template and are products. And you want the content editor to easily select product A,C & D but not product B without having to explicitly pick A, C and D from the list?. Ideally you want them to be able to say 'Get everything but B' and then A, C and D would all be selected but not B. Is that right? Commented Apr 27, 2017 at 14:54
  • 1
    Cool, yeah Id do something like that if I were you and auto-populate it somehow based on whatever logic you need to use. This means they just select one item then. Commented Apr 27, 2017 at 18:18
1

A possible solution apart from using the Query Builder is the Multilist with Search field Type.

In the source field you can specify the root to where the multilist should point and add a filter using the template name. enter image description here

the value in the source field should be like this: StartSearchLocation={11111111-1111-1111-1111-111111111111}&Filter=+_templatename:my template

  • {11111111-1111-1111-1111-111111111111} - replace with id of your container
  • my template - replace with your template name.

You can visit this blog to check the available filter options that can be used.

1
  • Would a content editor be able to exclude a specific item of the correct template type with this method?
    – Teeknow
    Commented Apr 25, 2017 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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