How to override Sitecore publish wizard Form

Image

The Sitecore publishing form sometimes needs to be modified with some custom messages, warnings and other custom functionalities like sending emails of the selected options in the publish form. In this blog, I am going to show how we can customize the Sitecore publish wizard form using Sitecore API and custom xml forms.There are some situations like we need to give custom warnings to the content authors about the potential problems in publishing certain content along with its child items (Home for example), logging the content author publishing activity to the admin, check for some custom permissions of the content author and show certain buttons or check boxes or performing some custom functionalities even before publishing has been performed.Solution The code written in this blog was tested for Sitecore 8.0, 8.1. Other versions of Sitecore May have different API code.Sitecore publish form is a form which has business logic in code beside class Sitecore.Shell.Applications.Dialogs.Publish.PublishForm which is in Sitecore.Client.Sitecore allows the developers to override the methods in this class.To override the default behavior of the publish form, we need to create a c# class and inherit Sitecore.Shell.Applications.Dialogs.Publish.PublishForm and override the following methods.

  1. ActivePageChanged
  2. ActivePageChanging
  3. OnLoad
  4. OnNext

In My example, I have overwritten the PublishForm and show the user a custom alert message when the user tries to publish Home page along with its children.I am overwriting the OnNext method of the Publish Form class and show the alert/confirm message to the content author. We can get the user selections on the publish wizard through HttpContext.Current. Request.Form. Based on the selected options by content authors, we can show the user confirm dialog using Sitecore Client Sheer Response API. Sheer Response contains method YesNoCancel which will let the developer to enter the custom message and width and height of window to be shown. PublishDialogProcessor method is the callback method which pass ClientPipelineArgs and will process the user dialog prompt process.Below is the code for overriding publish form.

using Sitecore.Data;using Sitecore.Data.Items;using Sitecore.Shell.Applications.Dialogs.Publish;using Sitecore.Web.UI.Sheer;using System;using System.Collections.Generic;using System.Web;namespace Sitecore.Digital.Web.Extensions.CustomPublishing{public class PublishWizardOverride : PublishForm{Sitecore.Data.Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");Sitecore.Data.Database webDb = Sitecore.Configuration.Factory.GetDatabase("web");Sitecore.Data.Database stagingDb = Sitecore.Configuration.Factory.GetDatabase("staging");Dictionary<string, string> changedContents = new Dictionary<string, string>();public bool PublishOnlyCurrentUser = false;protected override void OnNext(object sender, EventArgs formEventArgs){Item rootItem = UIUtil.GetItemFromQueryString(Context.ContentDatabase);//this IDs does not change with sitecore instance.bool userSelectedStaging = HttpContext.Current.Request.Form["pb_85867D55ACB84131B7AEB3AC356A4A14"] == "on" ? true : false;bool userSelectedWeb = HttpContext.Current.Request.Form["pb_8E080626DDC34EF4A1D1F0BE4A200254"] == "on" ? true : false;// if the user publishing home item and only to stagingbool publishChildren = HttpContext.Current.Request.Form["PublishChildren"] == "1" ? true : false;bool publishRelatedItems = HttpContext.Current.Request.Form["PublishRelatedItems"] == "1" ? true : false;string publishMode = HttpContext.Current.Request.Form["PublishMode"] ;ClientPipelineArgs clientArgs = new ClientPipelineArgs();clientArgs.Parameters.Add("id", rootItem.ID.ToString());clientArgs.Parameters.Add("name", rootItem.Name.ToString());clientArgs.Parameters.Add("publishChildren", publishChildren.ToString());clientArgs.Parameters.Add("publishMode", publishMode.ToString());Sitecore.Context.ClientPage.Start(this, "PublishDialogProcessor", clientArgs);}protected void PublishDialogProcessor(ClientPipelineArgs args){var id = args.Parameters["id"];var name = args.Parameters["name"];string publishMode = args.Parameters["publishMode"].ToString();string publishChildren = args.Parameters["publishChildren"].ToString();var rootItem = Context.ContentDatabase.GetItem(new ID(id));if (!args.IsPostBack){if(name.ToLower().Equals("home") && bool.Parse(publishChildren)){// Show the modal dialog if it is not a post backSheerResponse.YesNoCancel("You are about to publish the home item along with its children.It can take very long time.Do you wish to publish those items?", "500", "200");// Suspend the pipeline to wait for a postback and resume from another processorargs.WaitForPostBack(true);}else{base.OnNext(this, Context.ClientPage.CurrentPipelineArgs);}}else{// The result of a dialog is handled because a post back has occurredswitch (args.Result){case "yes":base.OnNext(this, Context.ClientPage.CurrentPipelineArgs);break;case "no":base.EndWizard();break;case "cancel":base.EndWizard();break;}}}}}

Sitecore default publish wizard form(Publish.xml) is in  \Website\sitecore\shell\Applications\Dialogs\Publish folder. It is an xml form with all the fields which was implemented in ‘Sitecore.Client’ class library and Sitecore.Shell.Applications.Dialogs.Publish.PublishForm class.Sitecore gives the developers ability to override the methods of PublishForm class.Follow the below procedure to override that.

  1. Copy the Publish.xml from the above specified folder.
  2. Create the folder under ‘\sitecore\shell\Override\Applications\Dialogs\publish’.
  3. Paste Publish.xml into the above created folder.
  4. Open Publish.xml and Modify the CodeBeside class of WizardForm Node to the class PublishWizardOverride which we just overwritten.
  5. CodeBeside attribute should contain the class name(PublishWizardOverride) with its namespace,Assembly name which you are developing the class.

 After following the above steps, Log into the sitecore and custom alert/prompt window will show up when we try to publish home item with its sub items.Below is the screenshot of the window.

SitecorePublishWizardForm