PowerShell Script to check if a Sitecore item exists in XMC environment endpoints

Remember my old post regarding a simple GUI-based tool that will check preview and edge endpoints in a Sitecore XMC environment to find if an item exists? 

Simple setup:

Preview Endpoint Querying Overview:


Edge Endpoint Querying Overview:



Tool in action:


Github Url

Now, here is a simple PowerShell version that uses the PSGraphQL module to go and fetch the item name from both preview and edge endpoints but is lightweight since you don't need to flex much to run a PowerShell script! So, now you can easily find if that nagging item is missing in edge endpoint and publish just the needed item.

Preview Endpoint querying overview:


Edge Endpoint querying overview:


######

param(
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
#Preview Endpoint URL for the environment
[string]$XmcPreviewEndPointUrl = "https://xmc-xyzbrands1abcd-xyzxm-sit.sitecorecloud.io/sitecore/api/graph/edge",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
#this is fixed url for edge
[string]$XmcEdgeEndPointUrl = "https://edge.sitecorecloud.io/api/graphql/v1",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
# Sitecore item id/path searched in the endpoints
[string]$SearchItemId = "{0D2FG595-9D70-4F3E-9C15-B161B22370C0}",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
#XMC env edge endpoint token
[string]$EdgeToken = "dsfsdfsfsfsfsfsfsfhdfsfsfsfdfsfsfsdfdsfsdfsssadfsdfVjY2FicmFuZHM4YjBiLW1lY2Nheasdadsadadada=",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
#SC Api key for the environment' preview endpoint
[string]$ScApiKey = "{fdgdgdgdfgdg-dfsdfsdf-sdf5-sdfs-sdfdsfssfs}",
[Parameter(Mandatory = $false)]
[ValidateNotNullOrEmpty()]
#language in which you are searching the item
[string]$Language = "en"
)
if(-not (Get-Module PSGraphQL -ListAvailable)){
Install-Module -Name PSGraphQL -Repository PSGallery -Scope CurrentUser
}
function GetItemfromPreviewEndpoint($url, $itemid, $lang,$scapikey) {
$url="${url}?sc_apikey=${scapikey}"
$uri = [System.Uri]$url
$query = '
query GetItem($id:String, $language: String!) {
GetItem: item(path: $id, language:$language){
id,
name
}
}'
$opName = "GetItem"
$variables = @{id=$itemid; language=$lang}
$result =Invoke-GraphQLQuery -Query $query -OperationName $opName -Variables $variables -Uri $uri
$result.data.GetItem.name | Format-Table
}
function GetItemfromEdgeEndpoint($url, $itemid, $lang, $edgetoken) {
$uri = [System.Uri]$url
$query = '
query GetItem($id:String, $language: String!) {
GetItem: item(path: $id, language:$language){
id,
name
}
}'
$headers = @{sc_apikey=$edgetoken}
$opName = "GetItem"
$variables = @{id=$itemid; language=$lang;sc_apikey=$edgetoken}
$result =Invoke-GraphQLQuery -Query $query -OperationName $opName -Variables $variables -Uri $uri -Headers $headers
$result.data.GetItem.name | Format-Table
}
Write-Host("Preview Endpoint:")
GetItemfromPreviewEndpoint $XmcPreviewEndPointUrl $SearchItemId $Language $ScApiKey
Write-Host("Edge Endpoint:")
GetItemfromEdgeEndpoint $XmcEdgeEndPointUrl $SearchItemId $Language $EdgeToken

######


PS in action:


Inputs in txt file:




Environment host name is nothing but the cms domain name in the cms url

Experience Edge key (EdgeApiKey):

Accessible at: https://deploy.sitecorecloud.io/projects/<project id>/environments/<environment id>/details?organization=<org id>

Ensure to substitute correct project id, environment id and org id in the above url.


ChatGPT:

To obtain the Experience Edge API key in the Sitecore XM Cloud Deploy app, follow these steps:medium.com+5doc.sitecore.com+5nehemiahj.com+5

  1. Access the Deploy App:doc.sitecore.com+3doc.sitecore.com+3doc.sitecore.com+3

  2. Select Your Project:medium.com+1youtube.com+1

  3. Choose the Environment:doc.sitecore.com+4nehemiahj.com+4doc.sitecore.com+4

    • On the project page, select the specific environment you're working with.
  4. Generate the API Key:doc.sitecore.com+2nehemiahj.com+2doc.sitecore.com+2

Comments