Wednesday, August 14, 2019

Writing a power shell script update the Url's the environment you are on

Scenario:
whenever you will get the latest content from Prod to QA.you will see some Url's still pointing to Prod.
below powershell script will find the particular Url and replace the Url of your current environment
it will traverse through all child items and replace the text wherever it is.it's time consuming if you have a big site.running the script by folder by folder,will be faster.

Using the bulk update below is the final script
$path="/sitecore/content/SomeSite/Home";
$findField="*http://www.Illinoiindinas.com*";
$originalText="http://www.Illinoiindinas.com";
$replaceWith="http://SitecoreQa.illinoindians.com";
$count=0;
$items=Get-ChildItem -Path $path -Recurse
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
foreach($item in $items){
    foreach($field in $item.Fields){
    if($field -like $findField)
    {
        $newValue= $field -Replace($originalText,$replaceWith)
        $item.Editing.BeginEdit()
        $field.Value=$newValue;
        $item.Editing.EndEdit() > $null
        $count=$count+1
        $StringToDisplay=[string]::format("Items updated {0} with fields {1}",$item.ID,$field.Name)
        Write-Host $StringToDisplay
    }
}
}
 Write-Host "Number Of Fields Updated:" $count
}


the above script is using the BulkUpdateContext,it will useful whenever you want to update the bulk content.and also you can add the security disabler. Sitecore.SecurityModel.SecurityDisabler

Point to Note:
        It's not always most efficient to operate on items by traversing the tree using Get-ChildItem
you can use the Sitecore QUery and Fast query as well,whenever working with the bulk items

Based on

No comments:

Post a Comment