Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label PowerShell. Show all posts
Showing posts with label PowerShell. Show all posts

Thursday, July 6, 2023

Sitecore PowerShell scripts as a datasource

Powershell script can be used in a data source to solve complex problems in Sitecore.

·       For this you can create script which is used to provide a list of child items of a parent item within a data source. For example, the script returns all the child items of the event folder that were created from the event detail template.

·       The script tag and full path of the script must be specified in the data source.

·       The script can be used in a template data source, a data source location, or a rendering data source.

·       The script must return either one item or a list of items for Sitecore to display to the end user.

 


Here is my YouTube video for the same

 


Stay tuned for more such articles, tips and tricks.

Thursday, July 18, 2019

Get all Sitecore page items only by using PowerShell script

We got the requirement to create report of all the items which are accessed by URL i.e. report of all page items.

A simple solution, for this, is to check is rendering exist on the an item or not?
If rendering exists, it means layout and components are assigned on that item. If rendering is empty, it means no layout or no component exists on that item.

You can write a C# code for generating such report but I think PowerShell is the best way to do this.

Here is the one line script for the same

Get-childItem -Path "master:\sitecore\content\home" -Recurse | Where-Object { ($_.__Renderings -ne "") } | Show-ListView -Property id, name, itempath

With this one line you can imagine the power of PowerShell. 

I strongly recommend to brush up your scripting skill.

Monday, December 3, 2018

Fixed : Sitecore 9.1 Installation Error - No registration found for extension 'OutNull' of type 'Task'

I was installing Sitecore 9.1 and started to receive error "No registration found for extension 'OutNull' of type 'Task'".

PS>TerminatingError(): "No registration found for extension 'OutNull' of type 'Task'."
>> TerminatingError(MapTasks): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: No registration found for extension 'OutNull' of type 'Task'."
>> TerminatingError(MapTasks): "The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: No registration found for extension 'OutNull' of type 'Task'."
Install-SitecoreConfiguration : No registration found for extension 'OutNull' of type 'Task'.
At C:\resourcefiles91\XP0-SingleDeveloper.ps1:74 char:1
+ Install-SitecoreConfiguration @singleDeveloperParams *>&1 | Tee-Objec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Install-SitecoreConfiguration
Install-SitecoreConfiguration : No registration found for extension 'OutNull' of type 'Task'.
At C:\resourcefiles91\XP0-SingleDeveloper.ps1:74 char:1
+ Install-SitecoreConfiguration @singleDeveloperParams *>&1 | Tee-Objec ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Install-SitecoreConfiguration

   
Reason:
I already have SitecoreInstallFramework version 1.2.1 installed and now I installed SitecoreInstallFramework 2.0.0
.
 

After that I run the "XP0-SingleDeveloper.ps1" script and I started to receive this error.

The root cause was that PowerShell load all the available modules when its start up. If you installed any module then it will not available in PowerShell immediately.

Solution:
I just restarted PowerShell
to load the newly installed module and run the "XP0-SingleDeveloper.ps1" script. This time my script completed successfully.

Happy Sitecore 9.1 Installation.

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.