Ensuring roles in Sitecore

Roles are easy to create in Sitecore but sometimes you might want to ensure that some specific roles always exists.

Not a very common scenario but nonetheless it happens. Once upon a time we needed to be able to ensure that a long list of specific roles always existed on the production instance otherwise some of our code and an integration to an external system could fail.

Inspired by the EnsureAnonymousUsers processor in the initialize pipeline I made the following small module. It is very similar to the code I shown in my previous post on how to disable the admin user.

First we make a new include config file, ensuredroles.config,  which contains a list of role names.

Then we implement a pipeline processor which iterates over the configured roles and creates them if they do not exist already.

And then we patch the processor into the end of the initialize pipeline.

That is it, yet another short post. The code can easily be extended to define roles-in-roles and the same principle can also be used for ensuring that specific users always exists. Note, remember to create any custom domains in Domains.config.

Avatar photo

Anders Laub Christoffersen

Anders has been working with Sitecore for over a decade and has in this time been the lead developer and architect on several large scale enterprise solutions all around the world. Anders was appointed the title of Sitecore Technical MVP in 2014 and has been re-appointed the title every year since then.

Leave a Reply

Your email address will not be published. Required fields are marked *