Skip to content

Instantly share code, notes, and snippets.

@michaellwest
michaellwest / ScanNavigationLinks.ps1
Created February 27, 2021 18:06
Find how many links will be queried when using the SXA Navigation component with a Sitecore PowerShell Extensions script.
@michaellwest
michaellwest / CleanupFields.sql
Last active October 19, 2021 10:16
Scripts for use with cleaning up Sitecore tables.
-- Find field data not associated with an item.
SELECT Id FROM SharedFields f
WHERE NOT EXISTS (SELECT ID FROM Items i WHERE i.ID = f.ItemId)
SELECT Id FROM UnversionedFields f
WHERE NOT EXISTS (SELECT ID FROM Items i WHERE i.ID = f.ItemId)
SELECT Id FROM VersionedFields f
WHERE NOT EXISTS (SELECT ID FROM Items i WHERE i.ID = f.ItemId)
@michaellwest
michaellwest / ScopeQueryToSearchStringModels.ps1
Last active December 6, 2020 04:04
Convert ScopeQuery to SearchStringModels using Sitecore PowerShell Extensions.
#$provider = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService([Sitecore.XA.Foundation.Abstractions.Configuration.IConfiguration[Sitecore.XA.Foundation.Search.SearchConfiguration]]).GetConfiguration().QueryMaxItems
$query = "+template:{51ed5851-1a61-4dae-b803-6c7fae6b43d8};+sxa:SameFirstTagAsCurrentPage|SxaTags;custom:EventStartDate|[NOW TO *];custom:EventEndDate|[NOW TO *];sort:eventstartdate;sort:isfeatured[desc]"
$modelList = [Sitecore.ContentSearch.Utilities.SearchStringModel]::ParseDatasourceString($query)
$contextItem = Get-Item -Path "master:" -ID "{31318AEE-310B-4EE4-84DA-4B84471E0FCA}"
[Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService([Sitecore.XA.Foundation.Search.Services.ISearchQueryTokenResolver]).Resolve($modelList, $contextItem)
class CustomSearchResultItem : SearchResultItem
{
[Sitecore.ContentSearch.IndexField("eventstartdate_tdt")]
@michaellwest
michaellwest / ReportAndRemoveBustedLanguageFields.ps1
Last active April 5, 2023 09:02
Find and delete fields where the language is empty using Sitecore PowerShell Extensions.
$sql = @"
SELECT DISTINCT i.* FROM [dbo].[Items] i
INNER JOIN [dbo].[VersionedFields] v
ON i.ID = v.ItemId
WHERE v.[Language] = ''
--SELECT COUNT(*) AS COUNT FROM [VersionedFields] WHERE [Language] = ''
--DELETE FROM [VersionedFields] WHERE [Language] = ''
"@
@michaellwest
michaellwest / CompareTwoIndexes.ps1
Last active October 22, 2020 15:10
Compare two indexes using Sitecore PowerShell Extensions to see what's missing.
# This is a ScopeQuery item found in SXA. You don't have to use this.
$scopeItem = Get-Item -Path "master:" -ID "{648F4C3A-C9EA-4FCF-82A3-39ED2AC90A06}"
# If not using SXA, you can replace this with the string from the QueryBuilder field type.
$scopeQuery = $scopeItem.ScopeQuery
$props = @{
Index = "sitecore_sxa_master_index"
ScopeQuery = $scopeQuery
}
$referenceList = Find-Item @props
@michaellwest
michaellwest / JoinSxaTags.html
Last active October 29, 2021 12:58
In Sitecore join SxaTags separated by comma using a Scriban template.
{{ if i_item.SxaTags != "" }}
<span>
Department:
</span>
{{ tags = [] }}
{{ for i_tag in (sc_followmany i_item "SxaTags") }}
{{ tags[tags.size] = i_tag | sc_field "Title" }}
{{ end }}
{{ tags | array.sort | array.join ", " }}
{{ end }}
@michaellwest
michaellwest / ImportRainbowItemBulk.cs
Last active October 4, 2020 18:31
Sample import command that uses threading for Sitecore PowerShell Extensions.
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Threading;
using System.Threading.Tasks;
using Kamsar.WebConsole;
using Rainbow.Model;
using Rainbow.Storage.Sc.Deserialization;
using Sitecore.Data;
@michaellwest
michaellwest / CreateRunspaces.ps1
Last active October 4, 2020 15:46
Create simple runspaces in PowerShell.
$watch = [System.Diagnostics.Stopwatch]::StartNew()
# BLOCK 1: Create and open runspace pool, setup runspaces array with min and max threads
$pool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS+1)
$pool.Open()
$runspaces = $results = @()
# BLOCK 2: Create reusable scriptblock. This is the workhorse of the runspace. Think of it as a function.
$scriptblock = {
param (
[string]$Data
@michaellwest
michaellwest / ExportAccess.ps1
Created October 1, 2020 17:15
Export and Import user access using Sitecore PowerShell Extensions
# Use this report to export current access
$users = Get-User -Filter * | Where-Object { $_.Roles.Count -gt 0 }
$records = [System.Collections.ArrayList]@()
foreach($user in $users) {
$record = [PSCustomObject]@{
"Username" = $user.Name
"Roles" = ($user.Roles | Select-Object -ExpandProperty Name) -join ","
}
$records.Add($record) > $null
@michaellwest
michaellwest / CleanupTables.sql
Last active February 12, 2024 01:27
Remove entries in the Sitecore Publishing Service queue using PowerShell.
Import-Function -Name Invoke-SqlCommand
$connection = [Sitecore.Configuration.Settings]::GetConnectionString("master")
$query = @"
DELETE FROM [Sitecore.Masterx].[dbo].[Publishing_ActivationLock]
DELETE FROM [Sitecore.Masterx].[dbo].[Publishing_Data_Params_FieldIds]
DELETE FROM [Sitecore.Masterx].[dbo].[Publishing_Data_Params_Languages]
DELETE FROM [Sitecore.Masterx].[dbo].[Publishing_JobManifest]
DELETE FROM [Sitecore.Masterx].[dbo].[Publishing_JobMetadata]