CMC & SEO
No Result
View All Result
Thursday, June 8, 2023
  • All about SEO
  • All about SEO
No Result
View All Result
BVMVacaville
No Result
View All Result
Home Uncategorized

XM Cloud: a contemporary method to content material administration and import with Authoring GraphQL API

by admin
March 15, 2023
in Uncategorized

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:

Token

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:

Powershell

The add instantly ends in Media Library on the requested path:

Result

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!

ShareTweetShare

Related Posts

Deposits, Deposits, Wherefore Artwork Thou? Juliet’s banker. 

June 7, 2023

Advertising and marketing Automation: Greatest Practices for Your Enterprise Success

June 7, 2023

Benefits of Magento and Salesforce Integration for Your E-Commerce Enterprise

June 7, 2023

Revamp Your Electronic mail Advertising Program with 5 Groundbreaking Options in SFMC Spring Launch ’23

June 7, 2023

SMS Advertising and marketing: A Nice Solution to Maximize the ROI of Your Enterprise

June 7, 2023

Find out how to Customise & Debug search engine optimisation Pages in Optimizely Configured Commerce

June 7, 2023

Popular News

The Greatest High Native search engine optimization Weblog Close to You 2020!

October 15, 2020

Dwell Running a blog the 2020 Election with Scalable Google Traits

November 3, 2020

🔥👶🏽🔥🕺🏽Leadferno

March 25, 2021

Google’s Query Hub: Want Nice Subject Concepts? Examine This Out

February 17, 2021

How To Monitor Passage Rating website positioning Efficiency in Google Search Console (And Why You Would possibly Need To)

February 11, 2021

Enjoyable, Straightforward Methods to Delight Your Prospects

September 24, 2020

What % Of Google SERPs Present Native Packs?

March 18, 2021

Easy methods to Enhance Your Conversions — Simply!

September 20, 2020
  • All about SEO

Copyright © 2021 CMC & SEO | All Rights Reserved

No Result
View All Result
  • All about SEO

Copyright © 2021 CMC & SEO | All Rights Reserved