Document based workflow approvers

#Kentico development #.NET #Kentico Kontent

SHARE

 
In this post I'm going to share with you my experience with overriding workflow approvers and make them configurable on the document level.

 

Recently I've got the request from one of my client to build assets management system with Kentico CMS. One of the main features of that system is workflow. There are sections of the site, those have to be approved by different approvers, which are nothing special, so I suggested them to build a workflow per section and assign appropriate approvers to it. Customer kindly rejected my suggestion, as they were planning to maintain thousands (if not hundreds of thousands) pages within the site, so there were too many section. Also sections were too small - sometimes that was single document.

The best solution in this scenario was to allow customer to setup approvers on the document level. 

Add fields for approvers to a page type

First thing I had to do was adding appropriate fields to the page type. In my case there were around 10 page types, but luckily, all they inherit the same base page type. Inheritance with page types is as much useful as it is with regular C# classes, so do not hesitate to use it whenever applicable.

In order to accomplish this task, navigate Page Types application, find appropriate page type and open edit scree for it. Go to Fields tab and add new field. Use following setting for your field:

 


Data type: text

Size: 200 or any other reasonable number in your particular case

Form control: Uni selector

Object type: cms.user

Return column name: username

Selection mode: Multiple

 

Feel free to configure the rest of the fields according to your needs.

There is one good reason to use object code name or GUID instead of object ID: if you're running synchronization using content staging, you don't have to implement object ID translation. You can find more details about translation here.

Add another field with the same settings, except object type - set it to cms.role.

Those two fields allow you to add users or roles, those should approve document, on the Form tab of particular document.

Override workflow permissions

Next step is to authorize those users to perform an action on a document. This requires overriding of workflow security. So now we need to implement custom WorkflowManager class and override CheckStepPermissionsInternal:

 


[assembly: RegisterCustomManager(typeof(CustomWorkflowManager))]
public class CustomWorkflowManager : WorkflowManager
{
    protected override bool CheckStepPermissionsInternal(TreeNode node, UserInfo user, WorkflowActionEnum action)
    {
        if (action == WorkflowActionEnum.Approve)
        {
            // get users allowed to approve from the document
            var users = node.GetStringValue(<usersField>, "").Split(';').ToList();
            approvers.AddRange(UserInfoProvider.GetUsers().Where(u => users.Contains(u.UserID.ToString())));
            if (approvers.Contains(user))
            {
                return true;
            }    
            
            // get roles allowed to approve document
            var roles = document.GetStringValue(<rolesField>, "").Split(';').ToList();            
            foreach (string role in roles)
            {
                if (user.IsInRole(role, currentSiteName))
                {
                    return true;
                }
            }
        }

        return base.CheckStepPermissionsInternal(node, user, action);
    }
}

 

As you can see, document, user and workflow actions are available in this method and this is all what you need in order to figure out whether current user is in approvers list or not. In my code I'm reading appropriate fields of the document in order to get approving users and roles and checking whether current user is in one of those lists. Pay attention to the last line of the method: if user is not in those lists of approvers, I'm running default check, so I'm not screwing up default Kentico permission check. Be sure to add validation to this method in case you are going to reuse it - code above is not final and was implemented only in demo purposes.

Conclusion

To summarize this article I want to admit that we went through using Uni Selector control, mentioned foreign key translation while staging and overridden default Kentico WorkflowManager class. All this allowed us to override the way Kentico handles workflow and gave us possibility to configure approvers for workflow on document level, which enables us to reuse the same workflow with different sets of approvers.

PLease feel free to share your experience with the related topic or ask your questions if any.

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