Surendra Sharma

Surendra Sharma

Search This Blog

Friday, November 16, 2018

Sitecore PowerShell script: Copy value of one field to another


I like Sitecore but I started to love Sitecore PowerShell. The way it’s interacting with Sitecore and allows developers to write command with C# code is amazing.

I have migrated SharePoint records in Sitecore in my earlier post.

This time I am bulk updating English and Arabic news items where news image field value is copied to another news fields “News List Image”.

cls
#input
$newsTemplateId = "{B69277AD-E917-4B9F-9136-A12E0A3E462F}"
$newsParentPath = "master:/sitecore/content/Habitat/Home/News"
$counter = 1

$allNewsItems = Get-ChildItem -Path $newsParentPath -Recurse
$filteredNewsItems = $allNewsItems | Where-Object { $_.TemplateId -eq $newsTemplateId }

foreach($newsItem in $filteredNewsItems) {
        if($newsItem.NewsListImage -eq "")
        {
            #Update English content
            $newsItem.Editing.BeginEdit()
            $newsItem.NewsListImage = $newsItem.NewsImage
            $newsItem.Editing.EndEdit()
   
            #Get Arebic Content    
            $newsArebicItem = Get-Item master: -ID $newsItem.ID -Language "ar-QA"
            #Check if Arebic content is exist
            if($newsArebicItem -ne $null)
            {
                #Update Arabic content  
                $newsArebicItem.Editing.BeginEdit()
                $newsArebicItem.NewsListImage = $newsArebicItem.NewsImage
                $newsArebicItem.Editing.EndEdit()
            }
            $counter
            $counter = $counter + 1
        }
        else
        {
            Write-Host "Image exist for item id " $newsItem.ID " and name " $newsItem.Name
        }
}

Feel free to use this script for bulk updating field values.

No comments:

Post a Comment