I have a headless SXA site and I was wondering if it’s possible to query an item under Settings via GraphQL. I think I should start with the query example here for getting the root item of a site but I’m not sure how to traverse the descendants from here.
3 Answers
To get the children of the context item, you can use the below query:
query ListOfChildren( $contextItem: String!)
{
contextItem: item(path: $contextItem) {
id
name
children{
id
displayName
}
}
}
To get the children of a specific item and filter children based on template, you can start with below query:
query RetrieveContentItems($itemPath: String = "/sitecore/content/Home") {
rootItem: item(path: $itemPath)
{
children(includeTemplateIDs: "{A87A00B1-E6DB-45AB-8B54-636FEC3B5523}")
{
id
name
}
}
}
You can find more details about GraphQL sample queries in these below blogs:
https://www.linkedin.com/pulse/useful-example-graphql-query-sitecore-context-arvind-gehlot http://amitkumarmca04.blogspot.com/2021/01/sitecore-graphql-queries.html#top
This is a common question when we start working on XM Cloud because we are used to work with the GetChildren()
method, but now we have to use GraphQL queries to achieve that. In the blog I refer below you will find how to use GraphQL queries to get the children of a particular item in the content tree. Basically you have to use the follow query:
query{
item(
path: "{XXXXXXXX-E896-433B-A8E7-7A2D8CAA0D4E}"
language: "EN"){
name
children{
total
pageInfo{
hasNext
endCursor
}
results{
Headline: field(name: "headline") {
jsonValue
}
}
}
}
}
The query shows the field "headline" of the children's item on the specified item path.
Here’s an example query to get the root item of a site and navigate to the Settings item:
{
item(path: "/sitecore/content/your-site") {
name
id
children(where: {name: "Settings"}) {
id
name
children {
id
name
fields {
name
value
}
}
}
}
}
item(path: "/sitecore/content/your-site")
: This gets the root item of your site.
children(where: {name: "Settings"})
: This looks for the child named Settings under the root item.
children { ... }
: You can further traverse the descendants under Settings to query specific items and their fields.
Make sure to replace /sitecore/content/your-site
with the correct path to your site's root item