0

I'm using this line of code to search for items whose names are exactly the string i provide in the query:

string query = path + "//*[@@templatename = 'Template Folder' and @@name='" + year + "']";

I'm trying now to find a solution to return items whose names at least contain this specific string.

Example : In my content tree i have 2 folders named "2018" I need to retrieve them in case i provide the term "201"

Is that possible ? I can't find any informations in the official documentation. Thank you in advance for your help

  • Can you use something like this: @@name='%" + year + "%' – Hishaam Namooya Sep 17 '18 at 16:36
  • Hey, i tried that in the XPath builder before posting the question and it seems it does not work unfortunately – Slrg Sep 17 '18 at 16:40
  • Just FYI, but this is a recursive sitecore query, which would search your entire content tree (extremely inefficient). You are better suited to use the content search api when search this many items. – Dylan Young Sep 18 '18 at 5:09
5

Sitecore query supports various axes, functions, and operators similar to those used in XPath. You can use The contains() function as follows:

string query = path + "//*[@@templatename = 'Template Folder' and contains(@@name, '" + year + "')]"

List of supported functions:

  1. contains('does this string', 'this string')
  2. startswith('does this string', 'this string')
  3. endswith('does this string', 'this string')
  4. not(condition)
  5. position()
  • Thank you for your answer. I get the following error : "Too many or to few arguments in Contains()" – Slrg Sep 17 '18 at 16:49
  • What is the second parameter by the way ? i just noticed there are 2. I don't get the difference – Slrg Sep 17 '18 at 16:51
  • Sorry, i updated my answer, i didnt change '=' to be comma – Ahmed Okour Sep 17 '18 at 16:52
  • Thank you very much. that's exactly what is was looking for! – Slrg Sep 17 '18 at 16:53

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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