In the first part of this series I explained how we can generate excel files of Sitecore media library and their public links and how can use these in Sitecore Content Hub in an import operation using the creation component, to pull these media items into Content Hub DAM, in this part I will show how you can update media library references/usage and replace image fields pointing to media library to point to Content hub using public links.
If you didn't go through the previous post I would highly suggest to check it out, as we will depend on one of its output to get the replace job done, so here is the link:
Now, if you remember, the first PowerShell script in the previous post, we got to pull all assets into Content Hub DAM and we created public links for these assets, in addition we get one extra CSV file which we will use to look for any usage of the media asset, this is one way of doing it, another way is to depends on the links database to get the usages of each single media asset (image, pdf...etc) using PowerShell as well and then replace the raw value of a field pointing to media item like below:
<image mediaid="{123e4567-e89b-12d3-a456-426655440000}" />
and replace it with a format of image field raw value pointing to Sitecore Content Hub DAM:
<image width='$($width)' height='$($height)' alt='$($alt)' src='$($imgSrc)' stylelabs-content-id='$($stylelabsId)' stylelabs-content-type='Image' thumbnailsrc='$($thumbnailSrc)' />
In the following Sitecore PowerShell we input two parameters, the first one is the csv file which contains the references/identifiers/public links from the first script, and the second param is the list of items to be updated (this can be generated through another PowerShell script), so basically you would need two CSV files as below:
- The first csv file is the one generated previously which contains the public links and identifiers
- The second csv file is a file contains the media item Id to the Content Item Id relation.
Here is the full PowerShell script:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$csvContenItemToMedia = Get-Item "/sitecore/content" | |
$csvPublicLinks = Get-Item "/sitecore/content" | |
# Note: shorten for the blog post. you would want tooltips. | |
$dialog = Read-Variable -Parameters ` | |
@{ Name = "csvContenItemToMedia"; Title = "CSV File Tile To Media"; Root="/sitecore/media library/"; Editor="item"}, ` | |
@{ Name = "csvPublicLinks"; Title = "CSV File Public Links"; Root="/sitecore/media library/"; Editor="item"} ` | |
-Description "This script will link overview tiles to images." ` | |
-Width 800 -Height 600 ` | |
-Title "Simple CSV Import Utility" ` | |
-OkButtonName "Import" ` | |
-CancelButtonName "Cancel" | |
if ($dialog -ne "ok") | |
{ | |
Exit | |
} | |
$baseUri = "https://test.sitecorecontenthub.cloud/" | |
$imgPublicLinkBase = $baseUri + "api/public/content/" | |
#your api key - go content hub > settings > users and generate api key for a user | |
$authToken = "aefcffe6b96f475db4a05c0b3e564c08" | |
$apiUri = $baseUri + "api/entities/identifier/" | |
$params = @{ | |
Uri = $apiUri | |
Method = 'GET' | |
Headers = @{ | |
'X-Auth-Token' = $authToken | |
} | |
} | |
# Read media stream into a byte array | |
[system.io.stream]$body = $csvContenItemToMedia.Fields["blob"].GetBlobStream() | |
try | |
{ | |
$contents = New-Object byte[] $body.Length | |
$body.Read($contents, 0, $body.Length) | Out-Null | |
} | |
finally | |
{ | |
$body.Close() | |
} | |
# Convert the stream into a collection of objects | |
$csvContenItemToMediaFinal = [System.Text.Encoding]::Default.GetString($contents) | ConvertFrom-Csv | |
# Read media stream into a byte array | |
[system.io.stream]$body1 = $csvPublicLinks.Fields["blob"].GetBlobStream() | |
try | |
{ | |
$contents1 = New-Object byte[] $body1.Length | |
$body1.Read($contents1, 0, $body1.Length) | Out-Null | |
} | |
finally | |
{ | |
$body1.Close() | |
} | |
# Convert the stream into a collection of objects | |
$csvPublicLinksFinal = [System.Text.Encoding]::Default.GetString($contents1) | ConvertFrom-Csv | |
#$_Products = Get-ChildItem -ID $ProductsRoot.ID -Recurse | Where-Object { $_.TemplateName -eq "Commerce Product"} | |
$foundCount = 0 | |
foreach ( $row in $csvPublicLinksFinal ) { | |
Write-Host 'Looking For >>' + $row.AssetToPublicLink | |
$relatedTiles = $csvContenItemToMediaFinal | Where-Object { $_.MediaId -eq $row.AssetToPublicLink} | |
foreach ( $row2 in $relatedTiles ) { | |
$foundCount = $foundCount + 1 | |
Write-Host 'Found >>' + $row2 | |
try { | |
$params.Uri = $apiUri + $row.AssetToPublicLink | |
#Write-Host $params.Uri | |
$apiResult = Invoke-RestMethod @params | |
$width = $apiResult.properties.FileProperties.properties.width | |
$height = $apiResult.properties.FileProperties.properties.height | |
$imgSrc = $imgPublicLinkBase + $row.RelativeUrl | |
$stylelabsId = $apiResult.id | |
#$item = Get-Item -I "master:" -Query "/sitecore/content//*[@SKU='$($row.AssetToPublicLink)']" | |
$TileItem = Get-Item -Path "master:" -ID $row2.TileId | |
$thumbnailSrc = $baseUri + "api/gateway/$($stylelabsId)/thumbnail" | |
$alt = $apiResult.properties.Title | |
$imgLink = "<image width='$($width)' height='$($height)' alt='$($alt)' src='$($imgSrc)' stylelabs-content-id='$($stylelabsId)' stylelabs-content-type='Image' thumbnailsrc='$($thumbnailSrc)' />" | |
if ($TileItem) { | |
$TileItem.Editing.BeginEdit() | |
$TileItem["Image"] = $imgLink | |
$TileItem.Editing.EndEdit() | Out-Null | |
Write-Host "$($TileItem.ID) updated" | |
} | |
else{ | |
Write-Host "Item not found - AssetToPublicLink: " + $row.AssetToPublicLink | |
} | |
} | |
catch { | |
Write-Host "Remove server didn't return 200 response for " + $row.AssetToPublicLink | |
} | |
} | |
Write-Host '===================' | |
Write-Host '' | |
} | |
Write-Host 'Found Count ==>> ' + $foundCount | |
Write-Host 'Total Public Link ==>> ' + $csvPublicLinksFinal.Count | |
Write-Host "Completed" |
Hopefully this will be useful, if you have any questions please feel free to reach out to me through comments or email.
No comments:
Post a Comment