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.

Saturday, November 10, 2018

Course : Learn React by building and deploying production ready app

There is one unstable factor in web technology and that's the shift of data processing on client and server side. 

With ASP, ASP.NET, JSP etc it was server side but within few years its shifted to client side due to JavaScript based frameworks. Every month I notice some new JavaScript based framework introduced.

Even Sitecore knows the importance of JavaScript based framework. That's the result of official launching and support of JSS.

Though I am not a big fan of JavaScript but somehow React.js impressed me more than Angular, knockout.

To update myself in recent scripting world, I completed course "Learn React by building and deploying production ready app" from Udemy.com

Learn React by building and deploying production ready app
Certificate : Learn React by building and deploying production ready app

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.

Saturday, October 27, 2018

Course : Data Science Essentials

Finished the course "Data Science Essentials" successfully on EDX offered by Microsoft by 98%.

Data Science Essentials
Data Science Essentials
I study below modules in this course
  • Module 1: Introduction to Data Science
  • Module 2: Probability and Statistics for Data Science
  • Module 3: Simulation and Hypothesis Testing
  • Module 4: Exploring and Visualizing Data
  • Module 5: Data Cleansing and Manipulation
  • Module 6: Introduction to Machine Learning