2

Is there any way to do the sorting by Sitecore content tree order? I am working on Sitecore SXA components to build the page. I am using search scope for list down the result. I have created the new index field for sort order field, but some of the items not updating properly and one more thing I would like to do the sorting irrelevant of folder structure.

**Parent folder**:

   *Child folder 1:*
           Item 1
           Item 3
   *Child folder 2:*    
           Item 6
   *Child folder 3:*    
           Item 5
           Item 4

So, I am expecting the order in Item 1, Item 3, Item 6, Item 5,Item 4. If I change the tree order, it should work irrelevant of child folders

3
  • I don't think this can be done ootb or with little coding. Maybe you can explain what the exact business requirement is because if you need to keep the folder structure you might have better solution than using search components.
    – Gatogordo
    Mar 1 at 14:16
  • @Gatogordo We have to sort the events irrespective of subfolder. I have more than 100 events, Event is my parent folder Local Event, Corporate Events, International events are my subfolders . Each subfolder should contain few events, so As per requirement Search page events list should be the sitecore tree structure, if the user change the sort by A-Z then we should change the order A-Z, that we can achieve using OOTB SXA. Mar 3 at 4:28
  • @Gatogordo I'm using Filters component from SXA search. What should do for this case? Mar 3 at 11:13

3 Answers 3

4

SXA provides 2 types of sort order OOTB -

  1. Sort by Names
  2. Sort by Count

But to achieve sort order as per your requirement, you need to create a custom sort order for that.

First, need to add __Sortorder in the index -

<sitecore>
    <contentSearch search:require="solr">
<indexConfigurations>
        <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
          <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
            <fieldNames hint="raw:AddFieldByFieldName">              
              <field fieldName="__Sortorder" returnType="long" />
            </fieldNames>
          </fieldMap>
 </defaultSolrIndexConfiguration>
      </indexConfigurations>
  </contentSearch>
  </sitecore>

Also need to remove this from excludedfield list -

<exclude hint="list:AddExcludedField">
              <__SortOrder>
                <patch:delete/>
              </__SortOrder>
            </exclude>

Now we create our facet to point it to the _Sortorder field. For that

  1. Navigate to your site facets node /sitecore/content/tenant/site/Settings/Facets
  2. Add new item using following template /sitecore/templates/Feature/Experience Accelerator/Search/Settings/Facets/IntegerFacet

enter image description here

Add the facet to the Sorting Group -

enter image description here

And select it on the Search Results component properties -

enter image description here

And here you have it, the results are now being sorted with the same order as the content tree.

You can follow the .below blog for that - https://www.sitecoreinsiders.com/sxa-search-results-sorting-by-sortorder-field/

3
  • I was thinking in the same direction, but this will not work with the subfolders. The sortorder is only valid within the folder - this sorting will not take the subfolders into account.
    – Gatogordo
    Mar 1 at 14:13
  • @Gatogordo that's a valid point, then in this case I think we can go with creating some logic in component-search-base-model.js and add custom sort order. Mar 1 at 14:19
  • @GauravAgarwal I could see some of the time sort order field is not updating, it is always empty, So should I need to do any config for that? Mar 3 at 15:48
0

If you need to display all the descendants from a parent folder taking into accounts the folder structure I would not use the search components.

You could try a pagelist but I'm not sure if that would keep the correct order as you want it.

So your best option might be to create a copy of promo. Feed it your parent folder as datasource and variant to display all children. You can use Query variants to loop over the folders and children or you can use Scriban with i_item.children.

0

The best I can think of as a solution for this is that you can build a ComputedField for sorting. The logic can utilize the SortOrder within the tree, and then you can combine it with Gaurav's answer.

3
  • The sortOrder field is not updating automatically after creating item. Should we need to configure anything for that? Mar 4 at 9:55
  • Yes, the SortOrder doesn't automatically update on create. I think you can add an event handler for the item:added event to update the SortOrder for the added item. Mar 4 at 11:07
  • Thank you Vinay, let me check Mar 4 at 11:37

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.