Recently we created an Events page using custom rendering variant in Sitecore SXA. Please look at it for better understanding – https://letsmakesitecoresimplecom.wordpress.com/2024/04/01/how-i-rendered-a-custom-template-in-sxa-without-writing-backend-code-with-sxa-rendering-variant/
Once an event expires, we need to automatically stop showing those events in website. To do this, we created a PowerShell script and automated running of this script using a Sitecore scheduler.
Lets look at the steps that we followed :
- Created PowerShell script as per our requirement like below. This script takes event parent item path and language id as input . Then iterates through children of that item & checks if the eventenddate field of each child item is less than today. If its an expired event, it makes that event unpublishable and publishes all child event
$item ="master:{{YOUR ITEM FOLDER PATH}}"
$langID = "{{LANGUAGE_ID}}"
$Items = Get-ChildItem $item -Language $langID -Recurse -Version Latest
$Today =[int] (Get-Date).ToString("yyyyMMdd")
foreach ( $child in $Items )
{
if(($child.TemplateName -eq "CME-Events") -and ($child["EventEndDate"]) -and ([int]$child["EventEndDate"].Split("T")[0] -lt $Today) -and($child["__Never publish"] -eq ""))
{
$child.Editing.BeginEdit()
$child["__Never publish"] = "1"
$child.Editing.EndEdit()
}
}
# Publish Item Event node with children
($item += $Items)|ForEach-Object {
$_.Editing.BeginEdit()
$_.Fields["__Workflow state"].Value= "";
$_.Editing.EndEdit()
Publish-Item $_ -Target "web" -PublishMode SingleItem
}
2. Go to script library under following path and save above created PS script- /sitecore/system/Modules/PowerShell/Script Library
3. Go to /sitecore/system/Tasks/Schedules and create a “PowerShell scripted task Scheduler”.
4. Name it as per convention, choose the script to execute(which we previously created), choose start and end date & choose time interval
5. If you have multiple schedulers running in Sitecore instance, make sure to choose “Async” checkbox . This option enables your Scheduler tasks to run asynchronous.
Hope it helps you!!
HAPPY SITECORING!! LETS MAKE SITECORE SIMPLE!!
Share the article to your LinkedIN Network using below options