Create Sitecore Ribbon Button using Sitecore PowerShell Extensions
Creating a custom Ribbon button in Sitecore using Sitecore
PowerShell Extensions (SPE) involves several steps, which I'll guide you
through below. The general process includes
- Create
PowerShell Script Module and Add Integration Point
- Setup
the PowerShell script Library and Add PowerShell script
- Configuring
the Ribbon button and Sync
- Verify
the button in the Ribbon
Here's how we can achieve that:
Pre-Requisites:
- Running
Sitecore Instance
- PowerShell
ISE to run PowerShell script
(Install compatible Sitecore.PowerShell.Extensions
if it’s not there)
Step-1: Create
PowerShell Script Module and Add Integration Point
- Create
PowerShell Script Module:
- Go
to Content Editor.
- In
the content tree, navigate to: System > Modules > PowerShell
> Script Library.
- Right-click
on Script Library and select Insert > PowerShell
Script Module.
- Give
it a name, such as CustomScriptsModule and click OK
- Add
Integration Point for Ribbon Button:
- Right-click
on the CustomScriptsModule we just created. We see different
options here, lets select
Insert > Integration
Point Libraries.
- Here
we see different Integration points, Select Content Editor > Ribbon
> Proceed.
Now we will
see the Item created under the CustomeScriptModule that we created in previous
step.
Step-2: Setup
the PowerShell script Library and Add PowerShell script
- Create
PowerShell Script Library
- Right-click
on the Developer Ribbon in newly created item (under the
module) and choose Insert > PowerShell Script Library.
- Give
any name like CustomeScriptLibrary
- Add
PowerShell script
- Right-click
on the newly created PowerShell Script Library, and choose Insert >
PowerShell Script.
- Provide
a meaningful name to the script, the same name command button will be
added to the Ribbon
- Elevate
the session if required.
- Provide
the login password if asked.
- Write the script: Now we can write PowerShell script that will be triggered by the Ribbon button. For instance, to show the item name when clicked, the script could look something like this:
# Get the current item
$item = Get-Item-Path .
# Show the item name in an alert
Show-Alert -Title $item.Name
Now we are done with the Script setup, its time to configure
the Ribbon button.
Step-3: Configuring
the Ribbon button
- Module
Activation:
- Go
back to the module we created in previous step
/sitecore/system/Modules/PowerShell/Script
Library/CustomeScriptModule
- In
the Module Activation section, click on the checkbox
Enabled – enables the module
for all integration points
- Rebuild
and Sync Library:
- Open
the PowerShell ISE (Integrated Scripting Environment) from the
Sitecore Desktop.
- Go
to Settings and then Rebuild All
- Select
Sync Library with Content Editor Ribbon. This will sync the new
button with the Ribbon UI in Sitecore. Once the processing is done, we
can verify the new button.
- Configure
Button Icon:
- Click
on the button (command - TestCommand)
- CONFIGURE
Ã
Icon Ã
set the icon
Step-4: Verify
the button in the Ribbon
- Go
to Developer Ribbon and we can see the new button created
- Click
on this button will run the script we written. For this case it will return
name of the selected item.
Example
Use Cases:
While the above example shows how to display an item’s name
when a button is clicked, we can also setup an interactive dialog that allows
user input and adapt the PowerShell script to perform various tasks. Here are a
few potential use cases:
- Create
a New Language Version of an Item: We can write a script to create a
language version of an item by using PowerShell commands.
- Delete
Specific Language Versions: A script could delete items in specific
languages by filtering by language and item path.
- Fetch
Data from an API and Save to Item: We can create a script that calls
an external API and updates Sitecore items with the fetched data.
Benefits:
- No
C# Required: We can directly write PowerShell scripts without needing
to setup any solution or write any C# code.
- No
Core Database Changes: This method allows us to create Ribbon buttons
without manually modifying the Core database.
- Flexible
and Simple: The integration of Sitecore PowerShell Extensions provides
a quick and simple way to add custom functionality to the Sitecore
interface.
Comments
Post a Comment