-
Custom Reset Layout in Content and Experience Editor Modes
February 9, 2018 Rodrigo Peplau 0
Recently I had to customize the code that triggers when a Reset Layout is executed. If you ever had to attach any code to it, this post is for you!
You know, that sympathetic prompt:
The first thing you need to know is that Content Editor and Experience Editor does that differently, so let’s go for them:
Round 1 – Content Editor
Content Editor uses a command for that (pagedesigner:reset), which is natively implemented by the class Sitecore.Shell.Applications.Layouts.PageDesigner.Commands.Reset. We first need to create a class that will inherit from the original, so we don’t lose the original behavior, and implement our custom logic on it.
Create a Command class like this:
using System.Web.Mvc; using Sitecore.Shell.Applications.Dialogs.ResetLayout; using Sitecore.Web.UI.Sheer; namespace MyProject.Commands { public class ResetLayoutCommand : Sitecore.Shell.Applications.Layouts.PageDesigner.Commands.Reset { protected override void Run(ClientPipelineArgs args) { // Runs the default behaviour base.Run(args); // Skips if the Reset Layout prompt is not submited or results is undefined if (!args.IsPostBack || string.IsNullOrEmpty(args.Result) || args.Result == "undefined") return; // Takes the item that has been reset var itemReset = DeserializeItems(args.Parameters["items"])[0]; // And here is where your custom logic will be implemented // ..... } } }Then we override the default command with our class in a config patch like this:
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <commands> <command patch:instead="*[@name='pagedesigner:reset']" name="pagedesigner:reset" resolve="true" type="MyProject.Commands.ResetLayoutCommand, MyProject"/> </commands> </sitecore> </configuration>And this is how we run the first leg. But it is not over yet, let’s go for…
Round 2 – Experience Editor
Experience Editor, on the other hand, uses a different approach for that. It has a request node named “ExperienceEditor.ResetLayout.Execute” under <sitecore.experienceeditor.speak.requests> that we need to replace. This request node is implemented by the native class Sitecore.ExperienceEditor.Speak.Ribbon.Requests.ResetLayout.Execute, which again we are going to inherit.
So our Request class will be like this:
using System; using System.Web.Mvc; using Sitecore.Diagnostics; using Sitecore.ExperienceEditor.Speak.Attributes; using Sitecore.ExperienceEditor.Speak.Server.Responses; using Sitecore.Shell.Applications.Dialogs.ResetLayout; namespace MyProject.ExperienceEditor { public class ResetLayout : Sitecore.ExperienceEditor.Speak.Ribbon.Requests.ResetLayout.Execute { [HasItemPermissions(Id = "{BE98D7F0-7404-4F97-8E4C-8FEF4ACA5DA3}", Path = "/sitecore/content/Applications/WebEdit/Ribbons/WebEdit/Advanced/Layout/Reset")] public override PipelineProcessorResponseValue ProcessRequest() { // Instantiates the returning object and var processorResponseValue = new PipelineProcessorResponseValue(); try { // Executes the default RESET LAYOUT process processorResponseValue = base.ProcessRequest(); // Takes the item that has been reset var itemReset = RequestContext.Item; // And here is where your custom logic will be implemented // ..... (OF COURSE, DON'T DUPLICATE YOUR LOGIC!) } catch (Exception e) { Log.Error( $"[ResetLayout] Cannot execute post Layout Reset operations to item '{RequestContext.Item.Paths.Path}'", e, GetType()); } return processorResponseValue; } } }And finally we patch it again with a config file like this:
<?xml version="1.0" encoding="utf-8" ?> <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/"> <sitecore> <sitecore.experienceeditor.speak.requests> <request patch:instead="*[@name='ExperienceEditor.ResetLayout.Execute']" name="ExperienceEditor.ResetLayout.Execute" type="MyProject.ExperienceEditor.ResetLayout, MyProject" /> </sitecore.experienceeditor.speak.requests> </sitecore> </configuration>And that’s all… You now have your custom code being triggered by both Content Editor and Experience Editor!
Categories: Content Edition Experience, Development, Experience Editor
Sitecore 9 Update 1 - Bug saving Shared Layout in Experience Editor Powershell script to find TDS files with length error
Localization
Recent Posts
Recent Comments
- NAVAN on Automatic Sitecore NuGet upgrades with Powershell
- Hedipo S Menezes on Corey Peplau wrote this - WFFM conflict with Unity DI and a lesson on how Sitecore community is so amazing
- Rodrigo Peplau on ERROR [Content Testing]: Cannot find PhantomJS executable at ' (...) /data/tools/phantomjs/phantomjs.exe'. Aborting screenshot generation.
- Fred on ERROR [Content Testing]: Cannot find PhantomJS executable at ' (...) /data/tools/phantomjs/phantomjs.exe'. Aborting screenshot generation.
- Rodrigo Peplau on Language specific MediaProvider breaking icons at Media Library
Archives
- February 2022
- December 2021
- November 2021
- March 2021
- July 2020
- February 2020
- September 2019
- July 2019
- April 2019
- March 2019
- December 2018
- February 2018
- January 2018
- November 2017
- September 2017
- August 2017
- July 2017
- March 2017
- February 2017
- November 2016
- September 2016
- August 2016
- July 2016
- April 2016
- November 2015
- September 2015
- July 2015
- April 2015
- March 2015
- February 2015
Categories
- Actions
- Active Directory
- Analytics
- Architecture
- Bug fixing
- CDP/Personalize
- Content Edition Experience
- Continuous Integration
- Dev
- Development
- Environments
- Experience Editor
- Experience Forms
- Front-end
- Health Check builds
- Helix
- How To
- LDAP
- MVP
- MVP Summit
- Phantom JS
- Powershell
- QA
- Richtext Editor
- Rules
- Security Provider
- SIF
- Sitecore 9
- Sitecore API
- Sitecore Community
- Sitecore Modules
- Sitecore Rocks
- Sitecore Rule Processor
- Sitecore Symposium
- SPE
- SPEAK
- SUG
- Support Ticket
- TDS
- Team City
- Uncategorized
- Upgrades
- Visual Studio
- WFFM
- Workflow
- XConnect
- xDB

