2

Environment: Sitecore 10.2 & SXA 10.2

Scenario: I would like to get the item by using its path or ID in Scriban.

Is there any OOTB functionality to achieve this?

2
  • Have you tried sc_query? doc.sitecore.com/xp/en/developers/sxa/93/…
    – Marek Musielak
    Nov 2, 2022 at 10:06
  • @MarekMusielak yes, I tried sc_query. This is the query I used: item = sc_query i_item (i_item.path + "/Data/Source") but it does not return anything. Also, I tried this syntax query:./ + path of the item.
    – PSS
    Nov 2, 2022 at 11:09

2 Answers 2

4

There is a Scriban function called 'sc_getitem' that exists in SXA 10.0+ that should do what you want. It allows you to get the item by either path or id and will use the context language by default. You can change the language if you want and I included an example below.

{{~ itemId = "{2C7654E0-6EE0-490A-A964-00EF71E4CAF7}" ~}}
{{~ itemPath = "/sitecore/content/Home" ~}}

itemById = (sc_getitem itemId)
itemByPath = (sc_getitem itemPath)
itemByPathAndLang = (sc_getitem itemPath 'de-DE')
1

You can get items by path as follows:

items = sc_query i_item "query:/$site/Home"

In the example above i'm using the $site token so I don't have to include the full path to my site. For a full list of available tokens, check Query Tokens in SXA.

You can get items by id as follows:

items = sc_query i_page "query:$site//*[@@id='{486B7B0B-A1B7-4735-AF43-8ACAD4C752D2}']"

Please note that sc_query always returns a list/array of items, so if you just want to use the first one returned, you can use the array.first command in scriban as follows:

item = (sc_query i_page "query:$home//*[@@id='{486B7B0B-A1B7-4735-AF43-8ACAD4C752D2}']") | array.first

For more info on how to use Sitecore Query, see https://doc.sitecore.com/xp/en/SdnArchive/Reference/Using%20Sitecore%20Query/Sitecore%20Query%20Syntax.html

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.