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?
Sitecore Stack Exchange is a question and answer site for developers and end users of the Sitecore CMS and multichannel marketing software. It only takes a minute to sign up.
Sign up to join this communityThere 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')
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
sc_query
? doc.sitecore.com/xp/en/developers/sxa/93/…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 syntaxquery:./ + path of the item
.