Override Sitecore Publish Wizard Form

This blog post is about customizing the Publishing Wizard to show custom messages while publishing items from Sitecore content tree. This custom messages will alert the content authors about the potential problems that may occur in publishing certain items along with its child items/related items like users who are trying to publish the root item(example: Sitecore/content) with its sub-items.

The Publish Wizard is a form which has business logic in class “Sitecore.Shell.Applications.Dialogs.Publish.PublishForm” which is in “Sitecore.Client” library. Sitecore allows the developers to override the methods in this class. To override the default behavior of the publish wizard form, we need to create a C# class and inherit “Sitecore.Shell.Applications.Dialogs.Publish.PublishForm” and we can override the following methods based on our requirement.

  • ActivePageChanged
  • ActivePageChanging
  • OnLoad
  • OnNext

In this example, I have overwritten the PublishForm’s  ‘OnNext’ method to show the Content Author a custom alert message, when they are trying to publish item of certain type like the root item(ex:Sitecore/content) along with its child items.

We can get the user selections on the publish wizard through “HttpContext.Current.Request.Form”. Based on the options selected by Content Authors we can display confirm dialog(using Sitecore Client Sheer Response API), in which the user has to select the required options to proceed further. The Sheer Response contains API method ‘YesNoCancel’ which will allow the developer to enter custom message, width and height of dialog window to be shown. The ‘PublishDialogProcessor’ method is the callback method which pass ‘ClientPipelineArgs’ and will process the user dialog prompt process.

Below is the code for overriding Publish Wizard form.

using Sitecore;

using Sitecore.Data;

using Sitecore.Data.Items;

using Sitecore.Data.Managers;

using Sitecore.Globalization;

using Sitecore.Shell.Applications.Dialogs.Publish;

using Sitecore.Web.UI.Sheer;

using System;

using System.Collections;

using System.Web;

 

namespace PublishFormCustomAlert

{

public class PublishWizardOverride: PublishForm

{

Sitecore.Data.Database masterDb = Sitecore.Configuration.Factory.GetDatabase(“master”);

protected override void OnNext(object sender, EventArgs formEventArgs)

{

Item rootItem = UIUtil.GetItemFromQueryString(Context.ContentDatabase);

bool 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(“PublishRelatedItems”, publishRelatedItems.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();

string publishRelatedItems = args.Parameters[“publishRelatedItems”].ToString();

 

var rootItem = Context.ContentDatabase.GetItem(new ID(id));

 

if (!args.IsPostBack)

{

if (bool.Parse(publishChildren))

{

string warningTemplateIds = String.Empty;

string[] warningTemplates = { };

string templateId = String.Empty;

Sitecore.Globalization.Language language = Sitecore.Globalization.Language.Parse(“en”);

Sitecore.Data.Items.Item publishWizardOverrideSettings = masterDb.GetItem(Constant.Settings.ItemID, language);

if (publishWizardOverrideSettings != null)

{

warningTemplateIds = publishWizardOverrideSettings.Fields[“WarningTemplateIds”].Value;

if (String.Compare(warningTemplateIds, “”) != 0)

{

warningTemplates = warningTemplateIds.Split(‘,’);

}

}

if (rootItem != null)

{

templateId = TemplateManager.GetTemplate(rootItem).ID.ToString().ToUpperInvariant();

}

 

if ((Array.IndexOf(warningTemplates, templateId) >= 0))

{

SheerResponse.YesNoCancel(“You are about to publish the market item along with its children.It can take long time. Do you wish to publish those items ?”, “500”, “200”);

// Suspend the pipeline to wait for a postback and resume from another processor

args.WaitForPostBack(true);

}

else

{

base.OnNext(this, Context.ClientPage.CurrentPipelineArgs);

}

}

else

{

base.OnNext(this, Context.ClientPage.CurrentPipelineArgs);

}

}

else

{

// The result of a dialog is handled because a post back has occurred

switch (args.Result)

{

case “yes”:

base.OnNext(this, Context.ClientPage.CurrentPipelineArgs);

break;

case “no”:

base.EndWizard();

break;

case “cancel”:

base.EndWizard();

break;

}

}

}

 

}

}

Note: The code written in this blog was tested for Sitecore 8.0, 8.1 and 8.2. Other versions of Sitecore May have different API code.

The below are the steps that should be followed  to override Sitecore Publish Wizard Form.

  • Copy the Publish.xml from the following path : \Website\sitecore\shell\Applications\Dialogs\Publish
  • Create an Override folder called ‘Publsih’ under “\sitecore\shell\Override\Applications\Dialogs\publish”
  • Paste the Publish.xml into the newly created Override folder ‘Publish’
  • Open the Publish.xml and update the CodeBeside class of WizardForm to the class “PublishWizardOverride” which we just created.
  • The CodeBeside attribute should contain the class name(PublishWizardOverride) with namespace (PublishFormCustomAlert), Assembly name in which you are developing the class.

Publish XML

After completing the above steps, whenever an user tries to publish an item from the content tree an custom alert/prompt window will be displayed. Below is an example screenshot when an user tries to Publish the root item with its sub-items.

Publish Wizard

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s