1

I'm using Sitecore 9.1 and SXA 1.8, I want to handle different actions on publishing an item in Sitecore. I was able to handle an action on clicking publish using OnNext() function:

 public class PublishWizardOverride : PublishForm
 {
    protected override void OnNext(object sender, EventArgs formEventArgs)
    {
        // Your code to display your popup goes here
        base.OnNext(sender, formEventArgs);
    }
 }

I also used event handlers to do actions onPublish, onPublishEnd. However my question is:

I want to add an action if a user clicks ok or cancel on the publish confirmation popup. Is there a way to do so?

enter image description here

1 Answer 1

2

You'll need to override the Publish Form code behind. The path to the publishing dialog can be found at website\sitecore\shell\Applications\Dialogs\Publish\Publish.xml.

You'll need to extend the PublishForm class and then override the ConfirmPublishingOptions method. Below is the code snippet

using System;
using System.Collections;
using System.Collections.Generic;
using Sitecore.Configuration;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Diagnostics;
using Sitecore.Globalization;
using Sitecore.Shell.Applications.Dialogs.Publish;
using Sitecore.Web.UI.Sheer;

namespace Sitecore.Custom.Publishing
{
    public class CustomPublishForm : PublishForm
    {
        protected override bool ActivePageChanging(string page, ref string newpage)
        {
            Assert.ArgumentNotNull((object)page, nameof(page));
            Assert.ArgumentNotNull((object)newpage, nameof(newpage));
            if (page == "Retry")
                newpage = "Settings";
            if (newpage == "Publishing")
            {
                if (GetLanguages().Length == 0)
                {
                    SheerResponse.Alert(Translate.Text("You must pick at least one language to publish."));
                    return false;
                }
                if (GetPublishingTargetDatabases().Length == 0)
                {
                    SheerResponse.Alert(Translate.Text("You must pick at least one publishing target."));
                    return false;
                }
                if ((this.PublishChildrenPane.Visible || this.Republish.Checked) && !this.ConfirmedOptions)
                {
                    Context.ClientPage.Start((object)this, "CustomConfirmPublishingOptions");
                    return false;
                }
            }
            return base.ActivePageChanging(page, ref newpage);
        }

        private static Language[] GetLanguages()
        {
            ArrayList arrayList = new ArrayList();
            foreach (string key in Context.ClientPage.ClientRequest.Form.Keys)
            {
                if (key != null && key.StartsWith("la_", StringComparison.InvariantCulture))
                    arrayList.Add((object)Language.Parse(Context.ClientPage.ClientRequest.Form[key]));
            }
            return arrayList.ToArray(typeof(Language)) as Language[];
        }

        /// <summary>Gets the publishing target databases.</summary>
        /// <returns>The publishing target databases.</returns>
        private static Database[] GetPublishingTargetDatabases()
        {
            ArrayList arrayList = new ArrayList();
            foreach (var publishingTarget in GetPublishingTargets())
            {
                string name = publishingTarget["Target database"];
                Database database = Factory.GetDatabase(name);
                Assert.IsNotNull((object)database, typeof(Database), Translate.Text("Database \"{0}\" not found."), name);
                arrayList.Add((object)database);
            }
            return arrayList.ToArray(typeof(Database)) as Database[];
        }

        private static List<Item> GetPublishingTargets()
        {
            List<Item> objList = new List<Item>();
            foreach (string key in Context.ClientPage.ClientRequest.Form.Keys)
            {
                if (key != null && key.StartsWith("pb_", StringComparison.InvariantCulture))
                {
                    Item obj = Context.ContentDatabase.Items[ShortID.Decode(key.Substring(3))];
                    Assert.IsNotNull((object)obj, typeof(Item), "Publishing target not found.");
                    objList.Add(obj);
                }
            }
            return objList;
        }

        protected void CustomConfirmPublishingOptions(ClientPipelineArgs args)
        {
            Assert.ArgumentNotNull((object)args, nameof(args));
            if (!args.HasResult)
            {
                bool flag1 = this.PublishChildren.Checked;
                bool flag2 = this.PublishRelatedItems.Checked;
                bool flag3 = this.Republish.Checked;
                string str = string.Empty;
                if (!string.IsNullOrEmpty(this.ItemID))
                {
                    if (flag2 & flag1)
                        str = Translate.Text("You are about to publish the current item, its subitems, and related items.");
                    else if (flag2)
                        str = Translate.Text("You are about to publish the current item and its related items.");
                    else if (flag1)
                        str = Translate.Text("You are about to publish the current item and its subitems.");
                }
                else
                    str += Translate.Text("You are about to republish the whole database.");
                if (flag3)
                {
                    if (!string.IsNullOrEmpty(str))
                        str += "\n\n";
                    str += Translate.Text("Republishing is an expensive operation that overwrites every item in the selected languages, even if that data has not changed.\nYou should only republish if the databases appear to be inconsistent and only after you have tried to fix the problem by performing a Smart Publishing operation.");
                }
                if (!string.IsNullOrEmpty(str))
                {
                    SheerResponse.Confirm(str + "\n\n" + Translate.Text("Do you want to proceed?"));
                    args.WaitForPostBack(true);
                }
                else
                {
                    this.ConfirmedOptions = true;
                    this.Next();
                }
            }
            else
            {
                //TODO - PERFORM YOUR CHANGES HERE
                if (!(args.Result == "yes"))
                    return;
                this.ConfirmedOptions = true;
                this.Next();
            }
        }
    }
}

The main changes happens at Context.ClientPage.Start((object)this, "CustomConfirmPublishingOptions");. You'll notice that from the default Sitecore, the method it is calling is ConfirmPublishingOptions.

I just updated the name and the code where you need to update is the else part of the method CustomConfirmPublishingOptions. You'll need to update where the TODO is found.

UPDATE

Once you have the code updated and deployed, you'll need to update the PublishForm codebeside.

  • Navigate to website\sitecore\shell\Applications\Dialogs\Publish\Publish.xml

  • Copy the Publish.xml and navigate to the path website\sitecore\shell\override\Applications\Dialogs. Create a folder named Publish and paste the Publish.xml

  • Update the codebesides to your namespace.

From

<WizardForm CodeBeside="Sitecore.Shell.Applications.Dialogs.Publish.PublishForm,Sitecore.Client">

to

<WizardForm CodeBeside="Your Namespace with className,Your Assembly">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.