Content Editor notifications with PowerShell

Reading time ~3 minutes

In my previous post I shared with you my small improvement for Sitecore schedule task.

If you haven’t read it I highly recommend you to do it first as this blog post will be about extending existing solution.

Schedule task status notification

New features are great as long as they are not problematic for people who are using them. To avoid possible confusion with my little improvement I’ve decided to put a clear notification when schedule task is disabled.

Sitecore provides very easy way to add Content Editor notifications via getContentEditorWarning pipeline. However this time I am not going to switch to Visual Studio. We can bring that functionality using SPE.

  1. Create your own Module with Content Editor Warning integration point checked,
  2. Navigate to your module and find Warning script library under Content Editor Warning node,
  3. Create new script beneath that node,
  4. Fill the script content with following piece of code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$currentItem = gi .
if([Sitecore.Data.Managers.TemplateManager]::GetTemplate($currentItem).InheritsFrom("SwitchableSchedule") -ne $true){
    return;
}

if($currentItem.Enabled -ne 1){    
    $title = "Your Sitecore schedule task is currently disabled"
    $text = "To Enable it please change 'Enabled' checkbox value"
    $icon = @{$true="Office/32x32/close.png";$false="office/16x16/media_play.png"}[$SitecoreVersion.Major -gt 7]

    $pipelineArgs.Add($title, $text);
    $warning = $pipelineArgs.Warnings[0]
    $warning.Icon = $icon
}

From now whenever you open scheduled task that is currently disabled you will see following notification

Notification options

In the previous section I added schedule task notification. Wouldn’t it be great to switch task status without looking for checkbox?

We have to modify our existing script by adding one option:

1
2
3
4
5
$id = $currentItem.ID
$db = $currentItem.Database.Name
$script = "{6DCA349D-BD9E-45CD-AD03-A72EDDEF46F5}"
$command = "item:executescript(id=$id,db=$db,script=$script,scriptDb=master)"
$warning.AddOption("Enable",$command)

Complete script with changed messages below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$currentItem = gi .
if([Sitecore.Data.Managers.TemplateManager]::GetTemplate($currentItem).InheritsFrom("SwitchableSchedule") -ne $true){
    return;
}

if($currentItem.Enabled -ne 1){
    $title = "Your Sitecore schedule task is currently disabled"
    $text = "To Enable it click button below"
     
    $icon = @{$true="Office/32x32/close.png";$false="office/16x16/media_play.png"}[$SitecoreVersion.Major -gt 7]
    
    $pipelineArgs.Add($title, $text);
    $warning = $pipelineArgs.Warnings[0]
    
	$id = $currentItem.ID
    $db = $currentItem.Database.Name
    $script = "{6DCA349D-BD9E-45CD-AD03-A72EDDEF46F5}"
    $command = "item:executescript(id=$id,db=$db,script=$script,scriptDb=master)"
    $warning.AddOption("Enable",$command)
	
    $warning.Icon = $icon
}

As you can see I’ve added option to a content editor warning object which allowed me to execute PS script. Script is simply changing the state of scheduled task item. Example code of executed script below:

1
2
3
4
$item = Get-Item -Path .
if($item) {
	$item.Enabled = $true
}

Results on the animation below:

Is there something else that can be improved? Of course!

Notice that I had to click the item in the tree to refresh state. I am pretty sure not everybody have to be aware of fact that notification status doesn’t change automatically when background action is executed. I’ve got solution for that too.

Use code below to add refresh button right below Execute action

1
2
3
4
$lang = $currentItem.Language
$ver = $currentItem.Version.Number
$command = "item:load(id=$id,language=$lang,version=$ver)"
$warning.AddOption("Refresh status",$command)

Bonus

While preparing blog post I found some neat cmdlets related to schedule tasks.

1
2
3
4
5
$item = Get-Item -Path .
if($item) {
  $schedule = Get-TaskSchedule -Item $item
  Start-TaskSchedule -Schedule $schedule
}

These can be used for another notification option or simply added to a context menu of schedule task item.

Now you can also test your task without touching schedule field or what is worse manipulating configuration files to decrease delay for database tasks agents.

If you are curious what else you can do with PowerShell inside Sitecore, project gitbook can be a great resource for you. I highly recommend that.

Summary

You may noticed already how deeply SPE integrates with Sitecore. Additional layer over features allows you to save your time and deliver features faster.
If you have to prototype something very fast (without rebuilding instance) this is the right choice.

XA Reference

Learn how to use SXA. XA Reference is an example website with preconfigured renderings and features. Continue reading

Cannot switch tabs in Experience Editor (SXA)

Published on February 08, 2017

Unicorn dependency graph generator

Published on January 24, 2017