Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label sitecore 9. Show all posts
Showing posts with label sitecore 9. Show all posts

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.

Friday, November 2, 2018

Sitecore PowerShell Script: Change alt value of image items in Sitecore media library


Sitecore PowerShell Script to change media items alt tag with image description or display Name or item name


cls
#input
$mediaTemplateName = "Image"
$mediaParentPath = "master:/sitecore/media library/Project"
$counter = 1

$allMediaItems = Get-ChildItem -Path $MediaParentPath -Recurse
$filteredMediaItems = $allMediaItems | Where-Object { $_.Fields["Alt"].Value -eq "" -and [Sitecore.Data.Managers.TemplateManager]::GetTemplate($_).InheritsFrom("Image") }

foreach($MediaItem in $filteredMediaItems) {
       
        $altValue = $MediaItem.ImageDescription

        if($MediaItem.ImageDescription -eq "" -and $MediaItem.DisplayName -ne "")
        {
            $altValue = $MediaItem.DisplayName.Replace(".jpg", "").Replace(".JPG", "").Replace(".png", "").Replace(".PNG", "").Replace(".gif", "").Replace(".GIF", "")
        }
        elseif($MediaItem.ImageDescription -eq "" -and $MediaItem.Name -ne "")
        {
            $altValue = $MediaItem.Name
        }

        #Update Alt tag
        $MediaItem.Editing.BeginEdit()
        $MediaItem.Fields["Alt"].Value = $altValue
        $MediaItem.Editing.EndEdit()

        Write-Host $counter " -> " $altValue " -> " $MediaItem.ItemPath
        $counter = $counter + 1
}

Any suggestions or improvements are most welcome.