Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael michaellwest

  • Texas
View GitHub Profile
@michaellwest
michaellwest / ScanNavigationLinks.ps1
Created Feb 27, 2021
Find how many links will be queried when using the SXA Navigation component with a Sitecore PowerShell Extensions script.
View ScanNavigationLinks.ps1
@michaellwest
michaellwest / CleanupFields.sql
Last active Feb 26, 2021
Scripts for use with cleaning up Sitecore tables.
View CleanupFields.sql
-- 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 Dec 6, 2020
Convert ScopeQuery to SearchStringModels using Sitecore PowerShell Extensions.
View ScopeQueryToSearchStringModels.ps1
#$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 Oct 22, 2020
Find and delete fields where the language is empty using Sitecore PowerShell Extensions.
View ReportAndRemoveBustedLanguageFields.ps1
$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 Oct 22, 2020
Compare two indexes using Sitecore PowerShell Extensions to see what's missing.
View CompareTwoIndexes.ps1
# 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 Oct 13, 2020
In Sitecore join SxaTags separated by comma using a Scriban template.
View JoinSxaTags.html
{{ 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 Oct 4, 2020
Sample import command that uses threading for Sitecore PowerShell Extensions.
View ImportRainbowItemBulk.cs
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 Oct 4, 2020
Create simple runspaces in PowerShell.
View CreateRunspaces.ps1
$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 Oct 1, 2020
Export and Import user access using Sitecore PowerShell Extensions
View ExportAccess.ps1
# 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 / DeleteFromPublishingJobQueue.ps1
Last active Jan 18, 2021
Remove entries in the Sitecore Publishing Service queue using PowerShell.
View DeleteFromPublishingJobQueue.ps1
Import-Function -Name Invoke-SqlCommand
$connection = [Sitecore.Configuration.Settings]::GetConnectionString("master")
$query = @"
DELETE TOP (1000)
FROM [dbo].[Publishing_JobQueue]
SELECT COUNT(*) QueueCount
FROM [dbo].[Publishing_JobQueue]
"@