Skip to content

Instantly share code, notes, and snippets.

@michaellwest
michaellwest / CompareFieldChanges.ps1
Last active February 3, 2023 15:18
The following Sitecore PowerShell Extensions script finds items stored in the IAR files that also exist in the database.
function Get-ModifiedItem {
param(
$databaseName,
$filename
)
$resourceLoaderType = ([System.Type]::GetType("Sitecore.Data.DataProviders.ReadOnly.Protobuf.IResourceLoader, Sitecore.Data.ResourceItems.ProtobufNet"))
$resourceLoader = [Sitecore.DependencyInjection.ServiceLocator]::ServiceProvider.GetService($resourceLoaderType)
$paths = [System.Collections.Generic.List[String]]@()
$paths.Add([Sitecore.MainUtil]::MapPath("/App_Data/items/$($databaseName)/$($filename)")) > $null
@michaellwest
michaellwest / HangfireRecurringJobs.ps1
Last active March 28, 2025 16:20
Precision scheduling for Sitecore using Hangfire. Replaces the out of the box Scheduled Task feature.
$connection = [Hangfire.JobStorage]::Current.GetConnection()
$recurringJobs = [Hangfire.Storage.StorageConnectionExtensions]::GetRecurringJobs($connection)
$props = @{
Title = "Hangfire Recurring Jobs"
InfoTitle = "Recurring Jobs Report"
InfoDescription = "This report provides details on the currently scheduled recurring jobs."
PageSize = 25
Property = @(
"Id",
@michaellwest
michaellwest / Find Fragmented Tables.sql
Last active August 8, 2022 13:31
SQL Maintenance Scripts for Sitecore
-- https://www.sqlshack.com/how-to-identify-and-resolve-sql-server-index-fragmentation/
SELECT S.name as 'Schema',
T.name as 'Table',
I.name as 'Index',
DDIPS.avg_fragmentation_in_percent,
DDIPS.page_count
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL, NULL, NULL) AS DDIPS
INNER JOIN sys.tables T on T.object_id = DDIPS.object_id
INNER JOIN sys.schemas S on T.schema_id = S.schema_id
INNER JOIN sys.indexes I ON I.object_id = DDIPS.object_id
@michaellwest
michaellwest / FindRevisionIssues.ps1
Last active May 2, 2024 14:17
Sitecore PowerShell Extensions script to rename items. Workaround for an issue in SXA where items are not published because the revision is missing on the item (even though the Content Editor shows one). Sitecore Support public reference number 522438
$matchedItems = [System.Collections.ArrayList]@()
$revisionFilter = @("9323dec0-9b37-4fae-b87c-2dc12cbea0f2")
Get-ChildItem -Path "master:\media library" -Recurse |
Where-Object { [string]::IsNullOrEmpty($PSItem["__revision"]) -or $revisionFilter -contains $PSItem["__revision"] } |
ForEach-Object { $matchedItems.Add([PSCustomObject]@{"ItemId"=$PSItem.ID; "RevisionId"=$PSItem["__revision"]; "ItemPath"=$PSItem.ItemPath}) > $null }
$matchedItems | Show-ListView
@michaellwest
michaellwest / .gitlab-ci.yml
Last active August 18, 2021 15:02
Example using GitLab to build a custom .net 5.0 SDK image using kaniko. We use kaniko to build the container in runtime this makes it so the building doesn’t require access to the docker daemon. This allows to utilize the normal runners and don’t have to enable privileged mode in docker.
stages:
- build:dotnet
default:
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
.job_template:
script: &script_definition
@michaellwest
michaellwest / SharePoint-HighlightRows.js
Last active June 22, 2021 17:32
Highlight rows in a SharePoint list.
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js></script>
<script>
jQuery(function() {
function applyStyles() {
jQuery(".ms-listviewtable tbody tr td:nth-child(3):contains('Required')").parent().css('background-color','red');
jQuery(".ms-listviewtable tbody tr td:nth-child(3):contains('Conditionally Required')").parent().css('background-color','blue');
jQuery(".ms-listviewtable tbody tr td:nth-child(3):contains('Additional Services')").parent().css('background-color','yellow');
jQuery(".ms-listviewtable tbody[id*='titl'] > tr > td > span").remove()
}
applyStyles();
@michaellwest
michaellwest / SortIPsByCount.ps1
Created May 14, 2021 15:31
Sort a list of IP addresses by count and IP.
$ips = @(
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"19.143.116.98"
"15.37.53.228"
@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")]