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:
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:
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
-
Access the Deploy App:doc.sitecore.com+3doc.sitecore.com+3doc.sitecore.com+3
- Navigate to the XM Cloud Deploy app at https://deploy.sitecorecloud.io/.medium.com+4nehemiahj.com+4doc.sitecore.com+4
-
Select Your Project:medium.com+1youtube.com+1
- In the navigation pane, click on Projects.doc.sitecore.com+1nehemiahj.com+1
- Choose the project that contains the environment for which you need the API key.
-
Choose the Environment:doc.sitecore.com+4nehemiahj.com+4doc.sitecore.com+4
- On the project page, select the specific environment you're working with.
-
Generate the API Key:doc.sitecore.com+2nehemiahj.com+2doc.sitecore.com+2
- Go to the Details tab within the selected environment.doc.sitecore.com
- Click on Generate Delivery API token. This action will produce the Edge API key required for accessing the Experience Edge Delivery API.nehemiahj.com+3doc.sitecore.com+3doc.sitecore.com+3
Comments
Post a Comment