Publish Items to Web and Edge with PSE script
Leonid Kryvoruchko
.Net Sitecore Consultant, 3x Sitecore Certified Developer (JSS, Headless, Content Hub, SXA, Composable Solution)
Hi there!
I decided to create a short article since I recently spent some time on a simple task, such as publishing items in Sitecore using Powershell Scripts.
It is supposed to be simple - read the documentation and write a couple of lines to do the job but since we are using both Web and Edge I had some issues with the last one.
Here is the code that works (Sitecore 10.3, Sitecore PowerShell Extensions 6.4.0.25771), hope it will save someone's time:
领英推荐
function PublishItems($itemsToPublish)
{
Write-Host "PublishItems: Count: $($itemsToPublish.Count )" -ForegroundColor Blue
Write-Host "Publish to Web..." -ForegroundColor Yellow
foreach ($item in $itemsToPublish)
{
Write-Host "Publish item: ID:" $item.ID " Path: " $item.Path
Publish-Item -Item $item.ItemRef -PublishMode Smart -Target "web" -Language $item.ItemRef.languages
}
Write-Host "Publish to Edge..." -ForegroundColor Yellow
$db = Get-Database "experienceedge"
foreach ($item in $itemsToPublish)
{
Write-Host "Publish item: ID:" $item.ID " Path: " $item.Path
#throws an error:
#Publish-Item -Item $item.ItemRef -PublishMode Smart -Target "experienceedge" -Language $item.ItemRef.languages
#works fine:
[Sitecore.Publishing.PublishManager]::PublishItem($item.ItemRef, $db, $item.ItemRef.languages, $false, $false, $false)
}
}
as you've noticed publishing to Edge is different since it throws some error if I try the same approach for Web.
In case you're interested in details here is a bug registered on git: https://github.com/SitecorePowerShell/Console/issues/1314, it stands that it is fixed, so maybe you are the lucky one with the fix ;)