In the case of content material administration, how would you take care of automating it?
You’ve in all probability considered Knowledge Trade Framework for organising content material import from exterior sources regularly, or Sitecore PowerShell Extensions because the common Swiss Military knife that permits doing every part.
Sitecore XM Cloud is a contemporary SaaS answer and due to this fact provides you yet one more means of managing and importing content material through GraphQL mutations. That is additionally an possibility for the most recent 10.3 XM/XP platforms bringing them a step nearer to the composable world of at this time.
Please welcome: Authoring and Administration API!
The documentation prompts a broad and awe-inspiring checklist of issues you are able to do with Authoring API in opposition to your occasion: create and delete gadgets, templates, and media. It additionally empowers you to do some operations round web site context and carry out a content material search in your CM occasion.
Administration API as well as offers you management over operations utilizing queries and mutations for the next GraphQL varieties:
Archiving
Database
Indexing
Job
Language
Publishing
Safety
Workflow
Guidelines
With that in thoughts, you may create and construction your content material, reindex, and publish it to Expertise Edge completely utilizing this API. So let’s check out the way it works!
Importing an image to Media Library utilizing GraphQL Authoring API
To begin with, it’s disabled by default, so we have to change by setting Sitecore_GraphQL_ExposePlayground environmental variable to true. Since these variables increase at construct time you additionally must re-deploy the environments
As soon as deployment is full, you can begin taking part in with it. Safety in a composable world sometimes works with OAuth, Authoring and Administration API shouldn’t be an exclusion right here. So as to receive an entry token, it’s essential authorize it first together with your shopper ID and shopper secret which you arrange with XM Cloud Deploy app:
There are alternative ways of authorization (for instance, utilizing CLI dotnet sitecore cloud login command), however since the necessity to absolutely automate the routine, I shall be utilizing /oauth/token endpoint. Additionally, it’s price mentioning that after getting initially already licensed with CLI, your shopper ID / secret pair is saved at .sitecoreuser.json file so let’s take it from there. Right here’s the code:
$userJson = “$PSScriptRoot/../../.sitecore/consumer.json”
if (-not (Check-Path $userJson)) {
Write-Error “The desired file ‘$userJson’ doesn’t exist.”
return
}
$userJson = Get-Content material $userJson | ConvertFrom-Json
$clientId = $userJson.endpoints.xmCloud.clientId
$clientSecret = $userJson.endpoints.xmCloud.clientSecret
$authorityUrl = $userJson.endpoints.xmCloud.authority
$viewers = $userJson.endpoints.xmCloud.viewers
$grantType = “client_credentials”
$physique = @{
client_id = $clientId
client_secret = $clientSecret
viewers = $viewers
grant_type = $grantType
}
$response = Invoke-RestMethod -Uri “${authorityUrl}oauth/token” -Methodology Put up -ContentType “utility/x-www-form-urlencoded” -Physique $physique
return $response.access_token
Now we bought the entry token and it needs to be handed as a header with each single request to GraphQL API:
“Authorization” = “Bearer <access_token>”
Subsequent, let’s make a mutation question that returns us a pre-signed add URL from passing API endpoint and a goal Sitecore path that you simply wish to add your media to. Right here’s the code:
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true, HelpMessage=”The URL of the endpoint where the file will be uploaded.”)]
[string]$EndpointUrl,
[Parameter(Mandatory=$true, HelpMessage=”The JWT token to use for authentication.”)]
[string]$JWT,
[Parameter(Mandatory=$true, HelpMessage=”The path of the file to be uploaded.”)]
[string]$UploadPath
)
$question = @”
mutation
{
uploadMedia(enter: { itemPath: “$UploadPath” }) {
presignedUploadUrl
}
}
“@
$physique = @{ question = $question} | ConvertTo-Json
$headers = @{
“Content material-Kind” = “utility/json”
“Authorization” = “Bearer $JWT”
}
# Invoke the GraphQL endpoint utilizing Invoke-RestMethod and cross within the question and headers
$response = Invoke-RestMethod -Methodology POST -Uri $EndpointUrl -Headers $headers -Physique $physique
$end result = $response.knowledge.uploadMedia
return $end result.presignedUploadUrl
Now having the pre-signed add URL, we will carry out media add passing the native file to course of:
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true, HelpMessage=”The URL to upload the file to.”)]
[string]$UploadUrl,
[Parameter(Mandatory=$true, HelpMessage=”The JWT token to use for authentication.”)]
[string]$JWT,
[Parameter(Mandatory=$true, HelpMessage=”The path to the file to be uploaded.”)]
[string]$FilePath
)
if (-not (Check-Path $FilePath)) {
Write-Error “The desired file ‘$FilePath’ doesn’t exist.”
return
}
$end result = & curl.exe –request POST $UploadUrl –header “Authorization: Bearer $JWT” –form =@”$FilePath” -s
$end result = $end result | ConvertFrom-Json
return $end result
This script will return the small print of a newly uploaded media merchandise, equivalent to:
merchandise title
merchandise full path
merchandise ID
I mixed all of the above cmdlets right into a single Demo-UploadPicture.ps1 script that cares about passing all of the parameters and performs the add operation:
The add instantly ends in Media Library on the requested path:
Professionals:
a contemporary platform agnostic method
works properly with webhooks
permits automating just about every part
wonderful administration choices making DevOps simpler
Cons:
cumbersome token operations
doesn’t permit batching, due to this fact takes a request per every operation
Verdict
It’s nice to have quite a lot of completely different instruments in your belt reasonably than having a single hammer in a hand with every part round turning into nails. Hope this new instrument brings your automation expertise to a brand new degree!