Skip to content

Instantly share code, notes, and snippets.

@michaellwest
Last active August 12, 2017 19:34
Example Sitecore PowerShell Extensions (SPE) Workflow Action script to post notifications into Slack using the PSSlack module.
$workflowEvent = $SitecoreContextItem | Get-ItemWorkflowEvent | Select-Object -Last 1
$previousState = Get-Item -Path "master:" -ID $workflowEvent.OldState
$currentState = Get-Item -Path "master:" -ID $workflowEvent.NewState
$user = Get-User -Id $workflowEvent.User
$subject = "Review Needed in Sitecore"
$message = @"
<strong>Details:</strong><br/>
$($user.Profile.FullName) ($($user.Name)) has transitioned '$($SitecoreContextItem.DisplayName)' from <i>$($previousState.Name)</i> to <i>$($currentState.Name)</i>.
"@
if($workflowEvent.CommentFields -and ($comments = $workflowEvent.CommentFields["Comments"])) {
$message += "<br/><br/><strong>Comments:</strong><br/>$($comments)"
}
$id = $SitecoreContextItem.ID.ToString().ToUpper()
$language = $SitecoreContextItem.Language.Name
$editUrl = "https://test.dev.local/sitecore/shell/sitecore/content/Applications/Content Editor.aspx?id=$($id)&amp;la=$($language)&amp;fo=$($id)"
$previewUrl = "https://test.dev.local/?sc_itemid=$($id)&amp;sc_mode=preview&amp;sc_lang=$($language)"
$message += "<br/><br/><a href='$($editUrl)'>Edit Item</a>"
$message += "<br/><br/><a href='$($previewUrl)'>Preview Item</a>"
Send-MailMessage -To test@test.com -From here@here.com -Subject $subject -SmtpServer localhost -Body $message -BodyAsHtml
Close-Window
# http://ramblingcookiemonster.github.io/PSSlack
Import-Module -Name PSSlack
$token = "[REPLACE_TOKEN]"
$workflowEvent = $SitecoreContextItem | Get-ItemWorkflowEvent | Select-Object -Last 1
$previousState = Get-Item -Path "master:" -ID $workflowEvent.OldState
$currentState = Get-Item -Path "master:" -ID $workflowEvent.NewState
$user = Get-User -Id $workflowEvent.User
$subject = "Review Needed in Sitecore"
$message = "*Details:*\n$($user.Profile.FullName) ($($user.Name)) has transitioned '$($SitecoreContextItem.DisplayName)' from _$($previousState.Name)_ to _$($currentState.Name)_."
if($workflowEvent.CommentFields -and ($comments = $workflowEvent.CommentFields["Comments"])) {
$message += "\n\n*Comments:*\n$($comments)"
}
$id = $SitecoreContextItem.ID.ToString().ToUpper()
$language = $SitecoreContextItem.Language.Name
$editUrl = "https://[REPLACE_SITECORE_URL]/sitecore/shell/sitecore/content/Applications/Content Editor.aspx?id=$($id)&amp;la=$($language)&amp;fo=$($id)"
$previewUrl = "https://[REPLACE_SITECORE_URL]/?sc_itemid=$($id)&amp;sc_mode=preview&amp;sc_lang=$($language)"
New-SlackMessageAttachment -Color $([System.Drawing.Color]::Blue) `
-Title "Preview Changes" `
-TitleLink $previewUrl `
-Text $message `
-Pretext $subject `
-AuthorName ($user.Profile.FullName) `
-AuthorIcon "[REPLACE_AVATAR_URL]" `
-Fallback "Please review and approve the changes at your earliest convenience." `
-MarkdownFields "text" |
New-SlackMessage -Channel '[REPLACE_CHANNEL_NAME]' -IconEmoji :heart: | Send-SlackMessage -Token $token
Close-Window
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment