While working through a SitecoreAI implementation recently, I ran into a small usability issue in the good ol' Sitecore Content Editor.
The Experience Editor and Preview buttons are still sitting there in the Publish ribbon, looking as inviting as ever.
There's just one problem:
They don't work. 😄
In the SitecoreAI environment I was working with, clicking either button resulted in the following error:
Connection to your rendering host failed with an Unauthorized error. Ensure the JSS Editing Secret is configured.
Rather than telling users to ignore them, I decided to repurpose them.
The goal was pretty straightforward:
- Replace Experience Editor with a SitecoreAI Page Builder button
- Replace the existing Preview behavior with a working SitecoreAI page preview
- Open the correct page for the currently selected Content Editor item
- Carry the current language and version into the request
- Support multiple sites and environments
- Do it without building or deploying any custom .NET code
Sounds like a job for Sitecore PowerShell Extensions.
The Existing Ribbon Buttons
The Content Editor ribbon definitions live in the Core database.
The two items we're interested in are:
/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Publish/Page Editor /sitecore/content/Applications/Content Editor/Ribbons/Chunks/Publish/Preview
Rather than creating entirely new ribbon controls, I opted to reuse these existing items and replace their behavior.
Each button's Click field can execute an SPE script using item:executescript.
For the Page Builder button:
item:executescript(
id=$ItemID,
script={YOUR-PAGE-BUILDER-SCRIPT-ID},
scriptDb=master
)
And for Preview:
item:executescript(
id=$ItemID,
script={YOUR-PREVIEW-SCRIPT-ID},
scriptDb=master
)
Now we just need to give those buttons something useful to execute.
Creating the SPE Scripts
I created a small module underneath the Sitecore PowerShell Script Library to keep everything together.
/sitecore/system/Modules/PowerShell/Script Library/
SitecoreAI/
Sitecore Pages/
Content Editor/
Ribbon/
Publish/
Publish/
Page Builder
Preview
Both entries are standard PowerShell Script items.
For my implementation, I used the following icons:
Page Builder /~/icon/apps/32x32/Paint.png Preview /~/icon/office/32x32/eye.png
Naturally, if choosing the perfect Sitecore icon becomes too difficult, I may know of a website that can help. 😄
Opening the Current Item in Sitecore Pages
The Page Builder script starts with the current Content Editor context item and builds the corresponding Sitecore Pages editor URL.
There are a few pieces of information we need along the way:
- Current item ID
- Current language
- Current version
- Site name
- SitecoreAI tenant
- Organization ID
In my case, the Sitecore solution contains multiple sites, so I determine the appropriate Sitecore site name based on where the selected item exists in the content tree.
Here's a sanitized version of the script:
The result is exactly what I was looking for.
Select a page in Content Editor, click SitecoreAI Page Builder, and the corresponding item opens directly in Sitecore Pages in a new browser tab.
No more hunting for the page again after switching applications.
Fixing Preview
Preview follows the same general idea, although constructing the URL requires a little more information.
In addition to the item, language, version, and Sitecore site, the script determines the route for the current item relative to the site's Home item and generates the appropriate rendering-host request.
Here's the sanitized version:
For the obvious reasons, I've replaced all tenant IDs, hosts, organization IDs, and secrets from my implementation with placeholders in the examples above.
The environment-specific SitecoreAI values can be retrieved from your Sitecore Cloud Portal / Deploy application.
Replacing the Ribbon Buttons
With both SPE scripts created, the last step is updating the original Core database ribbon items.
You can certainly update the fields manually, but since I needed to make the same change consistently between environments...PowerShell again.
The following script updates the Click, Header, and Tooltip fields on both existing ribbon items:
After updating the Core items, I rebuilt the Content Editor ribbon using the SPE utility available under:
Sitecore → PowerShell Toolbox → Development Tools → Rebuild ribbon integration points
Refresh the Content Editor and we're back in business!
Experience Editor is now SitecoreAI Page Builder, and the Preview button now launches a working preview for the selected item.
A Small Content Editor Quality-of-Life Fix
None of this changes how Sitecore Pages itself works, and it's certainly not some massive customization.
It simply makes the existing Content Editor workflow make a little more sense for users working in SitecoreAI.
If a button says Experience Editor and clicking it takes you to an error page, users are eventually going to ask why.
If we already know where they actually need to go, we might as well send them there.
And once again, SPE makes it possible to solve the problem without a custom deployment.
I hope this helps anyone else working through a similar SitecoreAI Content Editor setup!
Happy Sitecoring! ✌️

0 comments:
Post a Comment