3

I'm using the following query as a Source for my Droplink field:

query:fast:/sitecore/content/Home/AboutPages//*[@@templateid!='{41BF6376-A2D4-4CF1-80C5-223067C84811}']

My intention is for this query to grab all of the pages directly under AboutPages but exclude everything in Content folders on all levels (These content folders have a template ID of {41BF6376-A2D4-4CF1-80C5-223067C84811}). Is there a way to restrict this query to all page items aside from items located within Content folders? As of now, I'm not grabbing the Content folders themselves but I am grabbing all of the child items under these folders which I do not want to include.

Edit:

As an additional note, I cannot simply grab all pages under AboutPages by template ID because the pages themselves may have different templates.

Edit 2:

As a quick example I could have the following page structure:

  • About Pages
    • Page 1 (Template ID = "{53BC28A6-0A7D-4CDD-A53E-7AB78A20B5AF}")
      • Content Folder
        • Accordion
    • Page 2 (Template ID = "{81C65AE0-5BBB-4A99-BC7F-BCA463AFEE06}")
      • Page 3 (Template ID = "{881959CF-3617-4D36-9389-0DB2CA382F9A}")
        • Content Folder
          • Carousel
    • Content Folder

I need the query to return Page 1, Page 2, and Page 3 while excluding items in the content folder (Accordion, Carousel). My best guess for how to do this now is to find the template IDs of all possible pages and include those in the query but I was hoping for a more elegant solution.

  • 1
    Can you add some details about the tree structure. It might make it clearer what you are trying to achieve – Richard Seal Dec 7 '16 at 19:17
  • Agreed. Can you provide a screenshot of the tree and identify the items in the tree that you want to select with the query and the items you don't want to select? – DougCouto Dec 7 '16 at 19:18
  • I updated my answer. As I mentioned there, try turning it into a regular query by taking out the "fast:" part and see if it works any better. Maybe it's a limitation with fast query where if you exclude one item, the query no longer goes down that path. – DougCouto Dec 7 '16 at 19:29
  • It is possible to exclude items based on their parent: fast://some/path//*[@@templateid != '{41BF6376-A2D4-4CF1-80C5-223067C84811}']/*. In your case, though, I don't think it'll help you, since the pages directly under About Pages won't be selected with this approach. If you could start one level higher than About Pages, then there'd be potential to make it work. – Dmytro Shevchenko Dec 7 '16 at 21:41
2

Would it be an option to change the field type to a TreeList, allowing you to use the parameterized datasource syntax? This way you can set the datasource to "About Pages" and use the ExcludeItemsForDisplay to filter out the "Content Folder" items (and their children). Note that you must use the template name in this syntax, not the id.

So in the end something like this: Datasource=/sitecore/content/.../About Pages&ExcludeTemplatesForDisplay=ContentFolderTemplate

This would achieve what you want from the source point of view, but it will allow your editors to select more than one item. If that is an issue you could add validation on the field - a regex like ^({[^}]+}|?){0,1}$ should limit it to 0 or 1 selected item (guid).

Might not be the perfect solution as it changes the behavior for your editors but it is an option without custom code.

  • Perhaps a Droptree field would be a better choice rather than Treelist. It only allows selection of a single item and supports the enhanced query with parameters. – jammykam Dec 8 '16 at 0:03
  • 1
    That was my first thought, as all the (unofficial) documentation says the DropTree can handle this syntax as well, but I tested it (on 8.1 and 8.2) and the DropTree didn't take my ExcludeTemplatesForDisplay. – Gatogordo Dec 8 '16 at 8:29
  • I was getting an error, parsing "^({[^}]+}|?){0,1}$" - Quantifier {x,y} following nothing. I had to add an escape char, as so: ^({[^}]+}\|?){0,1}$ – Rob Scott Apr 12 '18 at 18:56
2

I need the query to return Page 1, Page 2, and Page 3 while excluding items in the content folder (Accordion, Carousel). My best guess for how to do this now is to find the template IDs of all possible pages and include those in the query but I was hoping for a more elegant solution.

You are correct, using the Sitecore Query notation, you cannot exclude items based on their parent; you would have to do what you mentioned which is add all of the Template IDs that you do want to include.

Alternatively, you could implement a custom source for your droplink field. Here's a blog post that does just that - https://ctor.io/dynamic-field-sources-with-getlookupsourceitems-pipeline/.

The getLookupSourceItems pipeline used in that blog post lets you build a custom query for your field so in your GetDataSource method, you could, for example, find all templates that inherit a "base page" template and then build your query using those ID's. That way, you don't have to manage this manually in Sitecore.

  • I just made an edit to my post, sorry I didn't specify this before. There can also be pages located under pages directly under AboutPages. For example you could have AboutPages > Content Folder, Page1, (Page2 > Page3, Content Folder). I need to return Page1, Page2, and Page3 which is located under Page2. – c-bro Dec 7 '16 at 19:13
  • Does the template of the pages the same? – Hishaam Namooya Dec 7 '16 at 19:18
  • The pages can be of a variety of different templates. – c-bro Dec 7 '16 at 19:24
  • So it does leave out all of the Content folders which are of template ID {41BF6376-A2D4-4CF1-80C5-223067C84811}. However, the query returns all items under the Content folder which I do not wish to include. The content folder could have an Accordion template and a Carousel template under it for example and those would be included in the query. – c-bro Dec 7 '16 at 19:32
1

I do believe that, in the long term, you should use one of the other answers posted. But as a quick fix, here's what you can do:

  1. Make sure that all templates that you want to be included in your Droplink field have the same prefix or suffix, e.g. Page. So your templates would be called like this:
    • ContactsPage
    • About Page
    • Testimonials Page
  2. Build your query to include items based on the suffix in their template name:

    fast:/sitecore/content/Home/AboutPages//*[@@templatename = '%Page']
    

This will exclude any item that doesn't represent a page.

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.