1

I would like to have a listing module where content editor could select from a list of article types (custom field on a custom template). The list should then query all articles for the selected articletype value.

If I was using a hardcoded item query it would be something like:

template:{53D07DD8-80B1-48CF-8E2C-BC36CFF7DC5B};+custom:articletype|Tips

Where Tips should correspond to the selection made by the content editor.

We don't want the content editor to have to go and create a new Item Query each type they wanted a different article type which is why I'd like the list to be driven by rendering parameters and selection fed into the query.

Using Sitecore 9.1 and SXA 1.8

Update: I have followed the suggestion of using a code datasource but I can't figure out how to pass in a filter value either through rendering parameters, or even on a user action.

eg: either content editor selects an article type from a drop down as a rendering parameter OR on the front end if we were to show a list of all, and allow the user to select an article type to show only those. It can't be handled in say javascript because then the pagination wouldn't match the displayed items.

Using a code datasource you can access the Sitecore.Context or the Item itself but can't seem to access anything to do with the rendering and would like to avoid putting the article type in the querystring of the url and having to do a postback.

  • What is the exact problem? Because as far as I can see you are asking for a ready solution. What have you tried so far? What problems do you have with picking up values from Rendering Parameters and passing them to your rendering controller/repository to fetch certain items? – Dawid Rutkowski Mar 27 '19 at 8:45
  • @DawidRutkowski I'm asking for best practice approach on solving this. I've tried using a page listing which you can tie to a query but how do you pass a rendering parameter to the query ie. the dynamics "Tips" part of the query. So I imagine I'm going about this wrong or trying to leverage the wrong sxa components. – Jen Mar 27 '19 at 23:04
2

For a search query you could create your own tokens but for an item query I ended up writing a solution for this based on a great article from Richard Seal on using code datasource item queries. You should read that ;)

Short version: it starts by knowing that you can actually use code:MyProject.Feature.X.., MyProject.Feature.X as a the query - e.g.:

enter image description here

You need to write this code. It has to be a class that implements Sitecore.Buckets.FieldTypes.IDataSource - in the public Item[] ListQuery(Item item) function you will have the code that return the list of items you want as a result of the query based upon the context item as parameter.

In this custom code, you can do almost anything.. in your case you could do an index query to find all articles that match a value on the context item. And if you need information on the current rendering, it is available in Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull.

|improve this answer|||||
  • Thanks for the link and the short version :) – Jen Mar 27 '19 at 23:10
  • Can I access the current rendering parameters from this class? I'm struggling to find how to reference. – Jen Mar 29 '19 at 1:19
  • I think you should be able to get them from the RenderingContext - something like RenderingContext.Current.Rendering["parameters"] .. (didn't test this) – Gatogordo Mar 29 '19 at 8:57
  • Unfortunately 'RenderingContext' does not exist in the current context. I can access the Sitecore.Context to get access to the HttpContext but that doesn't help me with the rendering parameters. – Jen Mar 31 '19 at 22:58
  • 1
    Added to the answer: all rendering information is available in Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull (tested it this time). – Gatogordo Apr 1 '19 at 8:48

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.