Thursday, August 11, 2022

Sitecore PowerShell Script Examples

 1. Update workflow of the Sitecore Items-

$WorkFlowItems = @(

@{ Recursive = $TRUE; Source = "master:/sitecore/content/xyz/Data" },
@{Recursive = $TRUE; Source = "master:/sitecore/content/abc/Items" }
);

 

ForEach ($Item in $WorkFlowItems)
{
if ($Item.Recursive)
{
$Sources = Get-ChildItem -Path $Item.Source -Language * -Version * -Recurse
ForEach ($source in $Sources)
{
$source.Paths.FullPath

 


if( $source.Fields["__Workflow"].Value -eq "{DFFF459CD2-MIPL-8956-89B0-XZ902C94FCE65}")
{
$workflowFinalState = "{BN678JK-BC67-7865-8K89-7653453F051U6}"
$source.Fields["__Workflow state"]
$source.Editing.BeginEdit()
$source.Fields["__Workflow state"].Value = $workflowFinalState

 

 

$source.Editing.EndEdit()
}
if( $source.Fields["__Workflow"].Value -eq "{HHK657J-785TC-5435-8HK67-2E413CF28654RH}")
{
$workflowFinalState = "{U675A3732-BN87-1234-8754-DRE3453901F83DB}"
$source.Fields["__Workflow state"]
$source.Editing.BeginEdit()
$source.Fields["__Workflow state"].Value = $workflowFinalState

  

$source.Editing.EndEdit()
            }
  

        }
    }

 }



 2. Update the Sitecore Items-


$contentItems=Get-ChildItem -Path master:/sitecore/content/ScItems/ -Language * -Recurse | Where-Object { $_.TemplateName -eq "TemplateName"}
forEach($contentPage in $
contentItems)
{

$contentPage.Editing.BeginEdit()
$contentPage.Fields["Title"].Value = $contentPage.Name
 

$contentPage
$contentPage.Editing.EndEdit()

}

 3. Read CSV and update the Sitecore items-

#file upload in temp folder
#create the folder if it is not found
$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$tempFolder = $dataFolder + "\temp\upload"
$filePath = Receive-File -Path $tempFolder -overwrite
 

if($filePath -eq "cancel"){
exit
}
 

$resultSet = Import-Csv $filePath 

$rowsCount = ( $resultSet | Measure-Object ).Count; 

if($rowsCount -le 0){
Remove-Item $filePath
exit
}

Write-Log "Item Update process started!";

foreach ( $row in $resultSet ) {
$currentItem = Get-Item -Path $row.ItemPath -Language $row.Language -ErrorAction SilentlyContinue
if ($currentItem){
$fields = $currentItem | Get-ItemField
foreach($field in $fields){
if($row -match $field) {
$currentItem.$field = $row.$field
}
}
}
else {
$logThis = "Couldn't find: " + $row.ItemPath + " with Language Version: " + $row.Language
$logThis
Write-Log $logThis
}
}

$logInfo = "Update process completed!";
$logInfo
Write-Log $logInfo

Remove-Item $filePath


 4. Content extract in excel-

$contentPages=Get-ChildItem -Path master:/sitecore/content/pages/ -Language "en" -Recurse | Where-Object {$_.TemplateName -eq "TemplateName"}

$resultObj = @()

forEach($contentPage in $contentPages )

{

$nameVal="";

$paramVal="";

$multilistField = [Sitecore.Data.Fields.MultilistField]$contentPage.Fields["FieldName"]

 

$multilistItems = $multilistField.GetItems()

 

foreach($multilistItemKey in $multilistItems){

$keyword=$multilistItemKey.Name+","+$paramVal

}

$multilistField = [Sitecore.Data.Fields.MultilistField]$contentPage.Fields["Allowed Countries"]

 

$multilistItems = $multilistField.GetItems()

 

foreach($multilistItem in $multilistItems){

$nameVal=$multilistItem.Name+","+$nameVal

}

$Obj = @{

itemname= $contentPage.Name

Content= $contentPage.Fields["AnyField"]

Data= $contentPage.Fields["AnyField"]

Text= $contentPage.Fields["AnyField"]

itemList= $nameVal

}

$resultObj += New-Object psobject -Property $Obj

}

$resultObj | Format-Table itemname, Content,Data,Text,itemList

Import-Function -Name ConvertTo-Xlsx

 

[byte[]]$outobject = $resultObj | ConvertTo-Xlsx

 

Out-Download -Name "FileName-$datetime.xlsx" -InputObject $outobject


5. Create Sitecore item versions-

/*Recursive = $TRUE if we want to create versions of child items also. Recursive = $FALSE if we don’t want create versions for child item. */

$ItemsToBeDeployed = @(

@{ Recursive = $FALSE; Source = "master: “give path to Sitecore item” "}
);



$languageParameters = @{

Language = "en"

TargetLanguage = @("fr","en-AU")

}

 ForEach ($Item in $ItemsToBeDeployed)

{

if ($Item.Recursive)

{

Get-ChildItem $Item.Source -Recurse | Add-ItemLanguage @languageParameters

}

else

{

Get-Item -Path $Item.Source | Add-ItemLanguage @languageParameters

}

}

No comments:

Post a Comment

How to Create a Public Link Using the Sitecore Content Hub REST API

Creating a public link using the Sitecore Content Hub REST API is a straightforward process that enables you to share content externally whi...