Saturday, March 24, 2018

Sitecore Powershell:: Dictionary Audit Series -2


Sitecore Dictionary Duplicate Keys

One of the major issue in the Sitecore Dictionary is Duplicate keys. For instance, if we have key called “optional” and we have created many dictionary items for the key “optional”. Even though we create many dictionary items for the same key, when we call Sitecore.Globalization.Translate.Text("optional"), this will return the firstordefault dictionary items in the dictionary tree.

Same key “optional”
  • Dictionary item 1-Translation:  This is optional text1
  • Dictionary item 2-Translation: This is optional text2
  • Dictionary item 3-Translation: This is optional text3

In the above example, every time and in all the place wherever we use “optional” dictionary key, this will show “This is optional text1”. Even though we change the text in Dictionary item 2 and 3, it will not reflect. This issue will become major if we have multilingual project, which means we will change the translation in one item but it will not get reflect in the page due to duplicate keys.

So in order to resolve this issue, I have written the Sitecore Powershell script to identify the duplicate keys.
As it is continuation of My previous blog, I have used the same fields like dictionary keys and the translate against each language in the our website and in addition to that I have added the Dictionary item id.

Script:
$root = Get-Item -Path (@{$true="master:\system\Dictionary\"; $false="master:\system\"}[(Test-Path -Path master:\system\Dictionary)])
$props = @{
        InfoTitle = "Dictionary Items"
        InfoDescription = "Lists of Dictionary Items"
        PageSize = 250
        Title = "Dictionary Items Report"
    }
$result = Read-Variable -Parameters `
    @{Name="root"; title="Items under"; Tooltip="choose the below path for searching dictionary items"} `
    -Description "This dialog shows search path of dictionary items" `
    -Title "Search Dictionary" -Width 500 -Height 300 -ShowHints

if($result -eq "cancel"){
    exit;
}
$items= [Sitecore.Data.Managers.LanguageManager]::GetLanguages([Sitecore.Data.Database]::GetDatabase("web"))

$reportItems = @()
$dictionaryitems = Get-ChildItem  -Path $root.ProviderPath -Recurse | Where-Object { $_.TemplateName -match "Dictionary entry"  }
if($dictionaryitems.Count -eq 0) {
    Show-Alert "There are no dictionaryitems"
} else {
foreach($dictionaryitem in $dictionaryitems){
$h=@{}
$h.add("ID",$dictionaryitem.Id)
$h.add("Key",$dictionaryitem.Fields["Key"])
foreach($item in $items){
if($dictionaryitem.Database.GetItem($dictionaryitem.ID,$item) -ne $Null ){
$h.add($item.Name,$dictionaryitem.Database.GetItem($dictionaryitem.ID,$item).Fields["Phrase"])
}
}
$reportItem = [pscustomobject]$h
$reportItems += $reportItem
}

$properties = $reportItems[0].PSObject.Properties | foreach-object {$_.Name}


$groupresult =$reportItems | Group-Object -Property Key | Where {$_.Count -gt 1}
if($groupresult.Count -gt 0) {
$groupresult.Group | Show-ListView -Property $properties
}
else
{
Show-Alert "There are no Duplicate keys"
}
}
Close-Window

How it works:

 Step 1:


 Step 2:

Step 3:

Step 4:



  
 Download the Sitecore package: Sitecore Dictionary Duplicate Keys

Saturday, March 17, 2018

Sitecore Powershell:: Dictionary Audit Series -1

List of Dictionary Items against each language on the website.

I’ve created this PowerShell script to get the report of the list of dictionary items against each language. It gets the input of the path which we need to get the report.

Fields in the report:
1.       Dictionary key
2.       All the languages in the web database. (My site has only English and Arabic)

Script:
$root = Get-Item -Path (@{$true="master:\system\Dictionary\"; $false="master:\system\"}[(Test-Path -Path master:\system\Dictionary)])
$props = @{
        InfoTitle = "Dictionary Items"
        InfoDescription = "Lists of Dictionary Items"
        PageSize = 250
        Title = "Dictionary Items Report"
    }
$result = Read-Variable -Parameters `
    @{Name="root"; title="Items under"; Tooltip="choose the below path for searching dictionary items"} `
    -Description "This dialog shows search path of dictionary items" `
    -Title "Search Dictionary" -Width 500 -Height 300 -ShowHints

if($result -eq "cancel"){
    exit;
}
$items= [Sitecore.Data.Managers.LanguageManager]::GetLanguages([Sitecore.Data.Database]::GetDatabase("web"))

$reportItems = @()
$dictionaryitems = Get-ChildItem  -Path $root.ProviderPath -Recurse | Where-Object { $_.TemplateName -match "Dictionary entry"  }
if($dictionaryitems.Count -eq 0) {
    Show-Alert "There are no dictionaryitems"
} else {
foreach($dictionaryitem in $dictionaryitems){
$h=@{}
$h.add("Key",$dictionaryitem.Fields["Key"])
foreach($item in $items){
if($dictionaryitem.Database.GetItem($dictionaryitem.ID,$item) -ne $Null ){
$h.add($item.Name,$dictionaryitem.Database.GetItem($dictionaryitem.ID,$item).Fields["Phrase"])
}
}
$reportItem = [pscustomobject]$h
$reportItems += $reportItem
}

$properties = $reportItems[0].PSObject.Properties | foreach-object {$_.Name}

$reportItems | Show-ListView -Property $properties
}
Close-Window

Step1:

Step 2:


 Step 3:

 Step4:


 Download the Sitecore package: List of Dictionary items against each language




Friday, February 23, 2018

SITECORE USER CLONE

Sitecore User Clone is used to create the copy of the user with same roles, name, email, and profile. It will prompt to accept the new username, password and all the editable fields same like Edit user.

Installation

Simply download and install the package for your Sitecore version.


Sitecore Items in the Packages:

·       Created the Clone User Application in Core Database under Security Application. Item Path –“/sitecore/content/Applications/Security/Clone User”.
·       Created a new button called Clone in User Manager ribbon to select the user. Item Path – “/sitecore/content/Applications/Security/User Manager/Ribbon/Home/Users/Clone”
Files in the Package:
·       In the App_Config/Include folder there will appear the SitecoreClone config file which has the command “usermanager:cloneuser”.
·       In the \sitecore\shell\Applications\Security\CloneUser there will be CloneUser XML file.
·       In bin folder has the Sitecore.UserClone.dll

Working:



We need to select the user to Clone the User.
     


Clone user Window:



The user needs to input the Username and Password. Other fields will be same as the selected user. A user can even edit the profile and change the profile. But before changing the profile, User needs to be created. All the current validation against creating user has been taken care of this clone user functionality.
Kindly post your comments and for contact sivakumar@outlook.com.


Saturday, February 10, 2018

Sitecore Powershell::Publish Powershell result Item

Sitecore Powershell:: Publish Powershell result Item

I wrote some the PowerShell scripts as a part of my Sitecore Powershell learning. To publish the result item, each item I need to open the particular item and then need to publish it. So I wrote a simple Publish action script which will intern calls the “item:publish” shell command and do publish.



Created a Publish Item Action script under Internal->List View->Ribbon->Item ->Publish Item.


The Script body contains the below simple code:

foreach($Item in $selectedData)
{
Invoke-ShellCommand -Name "item:publish" -Item $Item

}




Saturday, January 27, 2018

Sitecore 9 Installation Guide

Step by step Sitecore 9 Update 1 Installation guide:

As a part of my Sitecore knowledge Upgradation, I wanted to install Sitecore9 on my local system. Here are the steps that I followed the process.
In order to install the update, you can follow any of the below guides from Sitecore Architects.

Note: I followed the same steps as in the link - http://www.flux-digital.com/blog/install-sitecore-9-update-1-4-simple-steps/ and it works fine.
The below guide covers the complete installation following the same process, some of the issues faced by it and how it can be resolved. In addition, the log file has been attached as well with this guide to give a glimpse of the events and for tracking purpose.
 
PREREQUISITES:
Source: Sitecore 9 Update 1 Installation Guide
IIS Requirement – IIS10.0/IIS8.5
OS Requirement - Windows Server 2016, Windows Server 2012 R2 (64-bit), Windows 10 (32/64-bit), Windows 8.1 (32/64-bit)
.Net Framework Requirement - .NET Framework 4.6.2 or later.
JRE Requirement – Latest version
Visual Studio Requirement - Microsoft Visual Studio 2015 or later.
Database Requirement - Microsoft SQL Server 2016 SP1, Microsoft SQL Server 2014 SP2
Search Indexing Requirement - Solr 6.6.2(for Sitecore 9 update 0 – Solr 6.6.1), Azure Search
Hardware Requirement - 4 core processor, 16GB of RAM
  


Powershell script for installation:
Everyone needs automation in the installation process including me. So, our Sitecore Architects wrote the PowerShell Script to automate the installation process which makes our work easier. (But, if you are a person who feels that a mere installation is not enough and sheer understanding of the whole process is required you can consider reading the below section on the process explanation.)

Assumption: Create a folder “Sitecore9\Resources” under c:\ (where I had all my resource files)
Checklist:
Check 1: When I run the first Powershell Script, I got the below error:
.\Install-Solr.ps1: File C:\Sitecore9\Install-Solr.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\Install-Solr.ps1
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

·       In order to resolve this problem, run the below command:
PS C:\Sitecore9> Set-ExecutionPolicy RemoteSigned
·       The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the security risks described in the
about_Execution_Policies help topic at http://go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the execution policy?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): A
It will ask you for the Execution policy change. Give "A" and proceed.







Check 2: Before running the script check the Admin access
PS C:\Sitecore9>[bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
Check 3: SQL Server  sa username and password
Check 4: JRE version 1.8.0_162
Check 5:  Sitecore License file and copy to C:\Sitecore9\Resources.
Resource Files:
Download the below files and place all files under C:\Sitecore9\Resources:
2.       Sitecore 9.0.1 rev. 171219 (WDP XP0 packages).zip (https://dev.sitecore.net/~/media/8551EF0996794A7FA9FF64943B391855.ashx)
3.       After downloading Sitecore 9, unzip it.  Copy “Sitecore 9.0.1 rev. 171219 (OnPrem)_single.scwdp” and “Sitecore 9.0.1 rev. 171219 (OnPrem)_xp0xconnect.scwdp” to C:\Sitecore9\Resources.
4.       Unzip “XP0 Configuration files 9.0.1 rev. 171219.zip” and copy all the files to C:\Sitecore9\Resources.
At last the C:\Sitecore9\Resources path should look like this:



Now we are ready to run the scripts :
Step 1: Solr Installation
Since Solr was made as core requirement in Sitecore 9, we need to install solr in SSL mode(https). We are using Jeremy Davis Powershell script for installing the solr. In this script we need to mention the below:
    $solrVersion = "6.6.2", -- Version of Solr. We use 6.6.2 for Sitecore 9 update1
    $installFolder = "C:\Sitecore9\Solr", -  Here the solr will get installed
    $solrPort = "8983", - Default solr port
    $solrHost = "sc9solr", - Solr host name
    $solrSSL = $true, -- This should be true, we need to run solr in SSL mode(https)
    $nssmVersion = "2.24", -- NSSM version which we used to install solr as a service(don’t change it)
    $JREVersion = "1.8.0_162" --  JRE version in your local system ( run this command in cmd to find the version : java -XshowSettings:properties -version)
  $JREPath = "C:\Program Files (x86)\Java\jre1.8.0_162" (in the above command you can get the JRE path)
$downloadFolder = " C:\Sitecore9\Resources" – (The path where we downloaded and copied solr zip)

·       Copy the below script as “Install-Solr.ps1” in C:\Sitecore9
·       Open Powershell and run the script
PS C:\Windows\system32>cd \
PS C:\>cd Sitecore9
PS C:\Sitecore9> .\Install-Solr.ps1

The Solr has been installed successfully and you will see the Solr running in SSL Mode(https).


Step 2:  Install Sitecore Install Framework(SIF)
Here we are gonna use PowerShell script wrote by Flux-digital to install SIF.
·       Copy the below script as “Install-SIF.ps1” in C:\Sitecore9
·       Don’t change anything in this script and run in PowerShell.


Step 3:  Install Sitecore 9
·       Copy the below script as “Install-Sitecore9.ps1” in C:\Sitecore9
·       Define the below parameters in the script
$prefix = "sc91" – Instance name prefix
$PSScriptRoot = “C:\Sitecore9\Resources”  -- Path of all resource file
$XConnectCollectionService = "$prefix.xconnect" --  Xconnect name “sc91.xconnect”
$sitecoreSiteName = "$prefix.local" – Instance name “sc91.local”
$SolrUrl = https://sc9solr:8983/solr  -- Solr URL
$SolrRoot = "C:\Sitecore9\solr-6.6.2"  --  Solr Root Path
$SolrService = "solr-6.6.2"  -- Solr Service name
$SqlServer = "\"  -- SQL Server Data source name
$SqlAdminUser = "sa"  - sa username
$SqlAdminPassword = "****"   -- Sa password

·       Run the script and it will take nearly 10mins to execute each step.
·, At last, you can see that the Sitecore instance is up and running.



The below log files of Sitecore 9 Installation:

Saturday, January 20, 2018

Sitecore Powershell :: Reporting 'used image items with empty Alt field' using Alt tag Action

My client came up with a requirement to get the list of Media items with empty Alt field. In our existing Powershell package, we already have such reports.

But, when the requirement got refined to get the list of Used Media items with empty Alt Field (Because we have junk of media items which were unused) there were no options.
As a result,  I created the below scripts to get Used media items with Empty Alt field.
Create a Script under the path (/sitecore/system/Modules/PowerShell/Script Library/Content Reports/Reports/Media Audit/Used Image items with empty Alt field)




function HasReference {
    param(
        $Item
    )
    
    $linkDb = [Sitecore.Globals]::LinkDatabase
    $linkDb.GetReferrerCount($Item) -gt 0
}

function Get-MediaItemWithNoReference {
    $items = Get-ChildItem -Path "master:\sitecore\media library" -Recurse
 | Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder }
    
    foreach($item in $items) {
        if((HasReference($item))) {
   if($item."Alt" -eq '') {
            $item
        }
      }
    }
}

$items = Get-MediaItemWithNoReference

if($items.Count -eq 0) {
    Show-Alert "There are no used media items."
} else {
    $props = @{
        InfoTitle = $PSScript.Name
        InfoDescription = "Lists used media items that are not linked from other items."
        PageSize = 25
        Title = $PSScript.Name
    }
    
    $items |
        Show-ListView @props -Property @{Label="Name"; Expression={$_.DisplayName} },
            @{Label="Updated"; Expression={$_.__Updated} },
            @{Label="Updated by"; Expression={$_."__Updated by"} },
            @{Label="Created"; Expression={$_.__Created} },
            @{Label="Created by"; Expression={$_."__Created by"} },
            @{Label="Path"; Expression={$_.ItemPath} }
}
Close-Window


After running the script, I extracted the report with empty Alt tags.
But then I thought how they will be able input those bulk data from the report.
So, instead of opening each item, I came up with an idea to give them the Alt field against each media item. Hence, I created the Alt Tag Action under Item Ribbon.
(/sitecore/system/Modules/PowerShell/Script Library/Platform/Internal/List View/Ribbon/Item/Alt Tag)



foreach($Item in $selectedData)
{
    
$mediaItem = [Sitecore.Data.Items.MediaItem]$item;
$alt = $mediaItem.Alt;
$result = Read-Variable -Parameters `
   @{ Name = "alt"; Title = "Alt Tag";  Columns = 6 }   `
   -Description "Edit alt tag of '$($Item.DisplayName)'" `
   -Title "$($Item.DisplayName)" -Icon "Applications/16x16/photo_scenery.png"
                -Width 400 -Height 470 -OkButtonName "Change"
                -CancelButtonName "Cancel" -ShowHints;
  if ($result -eq "ok") {
  $Item.Editing.BeginEdit()
    $Item.Fields["Alt"].Value = $alt
    $Item.Editing.EndEdit()

 }
}


But this action was applicable for all the items, now what to do ??
Simple, restrict the rules only for Media items as below and it’s done ️.




Run the script now.







Select the media Item and Click Alt Tag Action.




Click change and open the Media item for the verifying the Input.



Download the Sitecore Package: Alt Tag Powershell Package

Thursday, January 11, 2018

Sitecore Powershell :: Redirect URL List


I wanted to make my hand dirty by exploring more on Sitecore Powershell, So I installed “Sitecore PowerShell Extensions-4.7 for Sitecore 8” Package and started looking into the ISE and console with existing Scripts. Suddenly something came to my mind. And, its the idea to list the Redirect URL in our site (Which may be huge in other sites).

Perquisites:   Redirect Module should have been used on your site.
Process:
Firstly, I created a new Redirect Powershell Script Module under Script Library.
And then, I created a Toolbox Script Library and then Redirect URL List Script.


In Scripting field, I wrote the below scripts:
# Get all the items recursively where the TemplateName equals "Redirect Url".
Get-ChildItem -Path master:\sitecore\system\Modules -Recurse | 
Where-Object { $_.TemplateName -match "Redirect Url"  } |
    Show-ListView -Property @{Label="Name"; Expression={ $_.DisplayName } },
  @{Label="Requested Url"; Expression={ $_.Fields["Requested Url"]} },
  @{Label="Response Status Code"; 
Expression={ $_.Database.GetItem($_.Fields["Response Status Code"]).Name} },
  @{Label="Redirect To Item"; 
Expression={ [Sitecore.Links.LinkManager]::GetItemUrl($_.Database.GetItem($_.Fields["Redirect To Item"]))} },
  @{Label="Redirect To Url"; Expression={ $_.Fields["Redirect To Url"]} }  `
        -Title "Redirect URL List"
        
Close-Window


After the completion of the Creation process, It should be listed in Powershell Toolbox as below.


Below is the result obtained on the click of Script created,:


Explanation:
This will extract all the items with the template name “Redirect URL”. In the List view property, the fields have been bound. To get the Redirect Item, I used Linkmanager that gives the Item URL.

Download the Sitecore Package: Redirect URL list Package