Skip to content

Instantly share code, notes, and snippets.

View fluxdigital's full-sized avatar

Adam Seabridge fluxdigital

View GitHub Profile
<#
### Creates Draft Sites for all existing Sites - for use on the CM Instance ###
.Description
For each site -> Duplicates the existing Site Grouping item and adds 'DRAFT' to the name.
Sets the environment to 'Draft' and database to 'master' also sets the target hostname to the current CM instance Url.
#>
$rootItem = Get-Item -Path "/sitecore/content"
$sxaSites = Get-ChildItem -Path "/sitecore/content/Sites"
$successCount = 0
<#
### Context Menu script to creates a single SXA Draft Site - for use on the CM Instance ###
.Description
Should be created in the Content Menu section, e.g: /sitecore/system/Modules/PowerShell/Script Library/{YOUR SCRIPT LIBRARY}/Content Editor/Context Menu
Duplicates the existing Site Grouping item and adds 'DRAFT' to the name.
Sets the environment to 'Draft' and database to 'master' also sets the target hostname to the current CM instance Url.
Moves the new Draft site to the top of the list so it is loaded before the non-Draft site.
#>
Import-Function -Name "Get-CmUrl"
Import-Function -Name "Move-Preview-Site-To-Top"
<#
### Moves all SXA Draft Sites to Top of list so they load on the CM before the non-Draft sites ###
.Description
Before running this script open the SXA Site Manager from the Powershell Toolbox to ensure all sites are up to date.
All Draft sites will be moved to the top of this field: "/sitecore/system/Settings/Foundation/Experience Accelerator/Multisite/Management/Sites"
#>
$sxaSitesList = Get-Item -Path "master:/sitecore/system/Settings/Foundation/Experience Accelerator/Multisite/Management/Sites"
$orderField = $sxaSitesList.Fields["Order"].Value;
<#
### Displays a 'Draft Site' message in Experience Editor ###
.Description
The $text variable below should be edited as required.
#>
$title = "Displaying Draft Site"
$text = "You are viewing the draft (Master DB) version of the site."
$icon = @{$true="Office/32x32/information.png";$false="Applications/16x16/warning.png"}[$SitecoreVersion.Major -gt 7]
<#
### Removes all existing SXA 'Draft' Sites ###
.Description
Finds all SXA Draft sites and deletes them.
#>
$sxaSites = Get-ChildItem -Path "/sitecore/content/Sites"
$successCount = 0
$failCount = 0
$siteGroupingPath = "Settings/Site Grouping"
<#
Function used to move preview site to the top of the list
#>
function Move-Preview-Site-To-Top($newSiteId){
$sxaSitesList = Get-Item -Path "master:/sitecore/system/Settings/Foundation/Experience Accelerator/Multisite/Management/Sites"
$orderValuesCurrent = $sxaSitesList.Fields["Order"].Value;
write-host "New Site ID: $($newSiteId)"
write-host "Current Order Values: $($orderValuesCurrent)"
<#
Function used to find the CM Url
#>
function Get-CmUrl($item){
$site = [Sitecore.Sites.SiteContext]::GetSite("website")
New-UsingBlock(New-Object -TypeName "Sitecore.Sites.SiteContextSwitcher" -ArgumentList $site) {
$urlOptions = [Sitecore.Links.LinkManager]::GetDefaultUrlOptions()
$urlOptions.AlwaysIncludeServerUrl = $True
@fluxdigital
fluxdigital / Index-Content.ps1
Last active September 14, 2022 16:22
Adds the items to the custom index
public int IndexContent()
{
var indexedCount = 0;
using (new SecurityDisabler())
{
try
{
//get the blog posts from Sitecore
var blogPostsRootPath = _master.GetItem(Settings.GetSetting("BlogPostsRootPath"));
public BlogPostSearchResultsModel GetResults(BlogPostSearchResultsModel blogPostSearchResultsModel, string searchTerm, int resultsPerPage, int currentPage)
{
using (new SecurityDisabler())
{
try
{
var solrCharsToAllow = "()-+*./' ";
var solrCharsToEscape = "(,),||,!,[,],^,~,?,:,-,+,\""; //split on comma
var safeSearchTerm = searchTerm.Trim().RemoveSpecialCharacters(solrCharsToAllow);
@fluxdigital
fluxdigital / BlogPostSearchIndexJob.cs
Last active September 15, 2022 23:34
SiteCron Job to index blog posts
public class BlogPostSearchIndexJob : IJob
{
private readonly ISearchService _blogPostSearchService;
private readonly ILogService _logService;
public BlogPostSearchIndexJob() :
this(ServiceLocator.ServiceProvider.GetService<ISearchService>(),
ServiceLocator.ServiceProvider.GetService<ILogService>(),
{