1

I want to search all the pages, based on "Template A" or "Template B" or "Template C" and page should be inside this location "/sitecore/content/Home/Services/"

I am trying to filter the pages with below code.

List<SearchResultItem> results = null;
using (var context = ContentSearchManager.GetIndex(sitecoreSxaWebIndex).CreateSearchContext())
{
   results = context.GetQueryable<SearchResultItem>()
       .Where(item => (item.Path.StartsWith("/sitecore/content/Home/Services/")) && 
           (item.TemplateName == "Template A") || 
           (item.TemplateName == "Template B") || 
           (item.TemplateName == "Template C"))
       .ToList();
}

Issue: It is filtering pages from outside the path which I have provided as well. Could you please help me with form the query to fulfill my combination of AND and OR condition?

Thanks in advance

1
  • By the looks of it if you are using SXA you should be using the services they provide to perform a search. SXA comes with a search service that will automatically narrow down results to the appropriate home item. There are other things in there too. Commented Mar 10, 2021 at 13:11

1 Answer 1

3

It seems like you got the couple of brackets wrong in your condition

List<SearchResultItem> results = null;
using (var context = ContentSearchManager.GetIndex(sitecoreSxaWebIndex).CreateSearchContext())
{
     results = context.GetQueryable<SearchResultItem>()
         .Where(item => item.Path.StartsWith("/sitecore/content/Home/Services/") && 
             (item.TemplateName == "Template A" || 
             item.TemplateName == "Template B" || 
             item.TemplateName == "Template C"))
         .ToList();
}

This should work.

2
  • Thanks Mahesh. It works
    – Jey
    Commented Mar 10, 2021 at 7:37
  • 2
    Please, do not use names when working with Sitecore items. Use IDs instead. If you will update the template name all your code will break. Commented Mar 10, 2021 at 7:53

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.