Adding confirmation to page actions of administration interface

#Kentico development #Kentico Kontent

SHARE

 
This post shows a few more things how Kentico global event handling can be extended to add more functionality to Kentico admin interface without need to modify Kentico built-in controls.

 

Let's imagine the situation that you need to show some javascript confirmation box to Kentico editor when he saves the data he has entered on the Form or Page tab of Kentico administration. You may ask what the reason is to show confirmation dialog when save the form? There might be a lot of tricky tasks and requirements that will require to do this. For example, you have a lot of documents on the site that are linked to different places under content tree. Editor of one site decided to modify content in some linked article. So you decided just to remind the Editor that he saves the linked document and if he don't want the changes influence all the linked documents the system will simply create a copy of this document. Another thing that might happen - you want to disable some header action (for example for some complicated workflow logic).

To implement such requirements you can find the file of the header action control in the Kentico project and add your logic to the code of that control. But as we may know modifying Kentico build-in controls is not really good practice as after upgrade to newer Kentico version your code might be rewritten and you will need to add this logic again. Do you agree? :)

So Kentico event handling is become handy here. More information about Kentico global event hanling might be found here: Handling global events. Code sample below demonstrate how to add javascript confirmation dialog on 'Save' action. You can access all page header action buttons and their properties and modify them accordingly. And it is all about subscription to OnAfterUserControlPreRender event and handling this event appropriately. You may add any required logic to that method according to the requirements you have. 

using System;
using System.Collections.Generic;
using CMS.Base;
using CMS.Controls;
using CMS.DocumentEngine;
using CMS.ExtendedControls;
using CMS.ExtendedControls.ActionsConfig;
using CMS.PortalEngine;
using CMS.SiteProvider;

[CustomizationLoader]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Custom attribute class for the CMSModuleLoader.
    /// </summary>
    private class CustomizationLoaderAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts.
        /// </summary>
        public override void Init()
        {
            //Assigns handler to display popup message
            EditMenu.OnAfterUserControlPreRender= EditMenu_OnAfterUserControlPreRender;
        }

        private void EditMenu_OnAfterUserControlPreRender(object sender, EventArgs e)
        {
            HeaderActions headerActions = sender as HeaderActions;

            if (headerActions != null && headerActions.ActionsList.Count > 0)
            {
                if (PortalContext.ViewMode == ViewModeEnum.EditForm || PortalContext.ViewMode == ViewModeEnum.EditLive || PortalContext.ViewMode == ViewModeEnum.Edit)
                {
                    List<HeaderAction> saveActionsList = headerActions.ActionsList.FindAll(p => p.EventName == "Save");
                    foreach (HeaderAction action in saveActionsList)
                    {
                        action.OnClientClick += "return confirm(\"Do you want to save the document?\");";
                    }
                }
            }
        }
    }
}
 

 

Author

Check other articles

5 5

What our clients say

Bits Orchestra team are outstanding developers‚Äč. They listen carefully to our business needs and easily turns our business objectives into a well thought out and executed development effort. Roman is very bright and definitely the most capable developer that has worked on our site. He is not only a Kentico expert but has successfully tackled other complicated development assignments demonstrating expertise in both front and backend development. Roman takes initiative to suggest enhancements that make site maintenance easier while improving the customer experience. The team is very responsive to our work requests and has great follow up. They have also worked very business partners and this has reflected positively on our company. Roman is a true partner for us and a tremendous asset to our organization. We will continue to work with them and would highly recommend Roman and his team for your development needs. He and his team will exceed your expectations!
 Alan Lehmann
Alan Lehmann
President at In energy sector

What our clients say

The Bits Orchestra team does excellent work. They are always available and I appreciate our frequent calls and screen-shares together. Their dedication to the projects and knowledge of Kentico is outstanding. They truly care about the quality of their work, and became a part of our team easily!
Shena Lowe
Shena Lowe
Managing Partner at Consensus Interactive

What our clients say

We hired Roman for a Kentico analysis project and have been very satisfied. He is very skilled and professional. We are looking to hire him and his team again on future projects.
Sylvain Audet
Sylvain Audet
CEO at MyDevPartner.com

What our clients say

Roman and team have taken over an existing Kentico EMS site for a large US Oil Company. So far, they have handled every single request that we have thrown at them and these were diverse, challenging, often bespoke, usually urgent and almost daily, over the last 11 months. Their work is of an extremely high quality, they are capable, quick and we have great confidence in the support that we are getting.
Jon Hollis
Jon Hollis
Head of Web Development at confidential

What our clients say

Bits Orchestra team was very helpful, they had a good understanding of the brief and deep knowledge of the system. They were always keen to provide advice and recommendations that benefit the project substantially.
Ramon Lapenta
Ramon Lapenta
Senior Front End Developer at Cyber-Duck Ltd