Query examples for authoring operations
You can perform authoring operations using queries and mutations for the following GraphQL types:
-
Item
-
Media
-
Search
-
Site
-
Template
For a complete list of available types and operations, refer to the built-in documentation in the GraphQL IDE.
Search criteria
The following sections describe the search criteria you can apply to your queries.
Exact
The search criterion is an exact match of the query term. For example, if you search for "apple", the query will only return results for which the exact value is "apple".
StartsWith
Returns a field value that starts with the specified search criterion. For example, searching for "app" returns both "apple" and "application".
Contains
Returns a field value that contains the specified search criterion. For example, searching for "app" returns "apple", "application", "happen".
EndsWith
Returns a field value that ends with the specified search criterion. For example, searching for "ple" returns both "apple" and "maple".
Wildcard
Includes a wildcard in the search criterion. For example, searching for "a*e" returns both "apple" and "angle".
Search
Searches for a full-text phrase within a value. For example, searching for "apple pie" returns items for which the value is "We made apple pie".
Range
Searches for a range of values in numerical and date fields. For example, you can search for any value between 1 to 5. When applying this criterion, also enter the value: a string defining the queried range within brackets. When entering the value, note the following:
-
The type of bracket determines whether the range is inclusive or exclusive. Square brackets denote an inclusive range, and round brackets denote an exclusive range. You can mix bracket types in a single query, for example: if the value is
[10 TO 20), 10 is included but 20 is excluded. -
Separate the lowest and highest values with the string
TO(case-sensitive). -
Use an asterisk * to denote an unrestricted value (no lower or upper limit). For example, if the value is
(10 TO *), 10 is excluded, and there is no upper limit.
Fuzzy
Used to find matches that are similar to the search term, allowing for minor differences such as typos or variations in spelling. It uses similarity parameters (a value between 0 and 1) to determine how close the match should be to the search term. A higher value means stricter matching. For example, searching for "example" with a similarity parameter of 0.8 might match "exampel" and "exmple".
Proximity
Used to find documents where the search terms might be separated from each other by other words. The number of separating words is designated with parameters. For example, searching for "apple pie" with a proximity parameter of 5 returns "apple is not a pie".
RegEx
A search term that applies a regular expression (RegEx). For example, searching for "a.*e$" returns "apple" and "angle".
Query examples
The following sections contain example queries for authoring operations.
Item
Create an item
You can create an item with the createItem mutation.
Query
Result
Delete an item
You can delete an Item using the deleteItem mutation. You can specify the item ID or path. If you specify both, the path parameter is ignored. You can request a permanent deletion by setting the permanently parameter value to true in the request input.
Query
Result
Get an item
You can use the item query to retrieve an item. This query retrieves a single item based on the supported identifier arguments, such as itemId.
Query
Result
Update an item
You can use the updateItem mutation to update item fields.
Query
Result
Copy an item
You can use the copyItem mutation to create a copy of an existing item.
Query
Result
Template
Create a Template item
You can use the createItemTemplate mutation to create the item template.
Query
Result
Update a Template item and its fields
You can use the updateItemTemplate mutation to update the item template.
Query
Result
Media
Upload media
You can use the uploadMedia mutation to get a pre-signed upload URL. You use the pre-signed upload URL to upload a media file to Sitecore.
Query
Result
Some code examples, images, and UI labels may still use XM Cloud while engineering assets are being updated.
If the query for a pre-signed upload URL returns an error with the message The specified key is not a valid size for this algorithm, check if the GraphQL.UploadMediaOptions.EncryptionKey setting has a value. For example:
If it does not, you must set it.
After you get the pre-signed upload URL from the response, you can send a POST request to upload the media file to Sitecore.
For example, by using the curl client:
The response to the request is a JSON-formatted response with the media item ID, Name, and Full path on successful media upload.
Site
Get the configured site
You can query information about a specific site by its name.
Query
Result
Search
Search items under a specific path
You can search through all the items using the search query. In addition to the example shown, you can use various system fields in your query, such as _name, _template, _templatename.
Query
Result
Sort search results by created date
You can sort search results by a field value.
Query
Result
Configure search results using paging
By default, the query returns the first ten search results. You can change this by adding paging parameters to your query.
To do so, use the following parameters:
-
pageSize- the number of results per page. -
skip- the number of results to skip. -
pageIndex- the name of the search results page index to return.
Query
Results
Validation
Get validation results for an item template
You can get details about potential validation errors for a field in a field template by querying the validation item.
Query
Result
Examples of multiple queries
This section describes a series of authoring operations for managing content.
Get all page content
You can make the following three queries to get all the contents of a page. This can be useful if you are building a custom solution, such as a Sitecore Marketplace app, that requires retrieving text content from a page and details such as the last modified date and the user that last edited the content.