Migrating Sitecore Roles Using Powershell

Fun Fact: Simply packaging Sitecore roles up doesn’t really move them.

Sitecore’s use of the .NET Membership Provider (I’m guessing) means that user roles behave oddly when doing things like serializing or packaging them. This is because all of the info about a given role doesn’t actually live on that role’s record.

The “Member Of” information about a role is stored on the role that a given role is a member of, as opposed to on the member role. This means that if you serialize (including using TDS) or package a role, the data about what role that role belongs to does not come along with it.

This can be very frustrating when trying to deploy roles to different environments and keep them consistent. Yes, you could theoretically package up all the roles but that exposes another fun quirk of the role system: you cannot update existing roles when you install a package containing them. That, of course, is a rather annoying issue because the most common use of roles, at least in my experience, is to bundle up a bunch of default Sitecore roles to ease user management. In that scenario, Sitecore won’t update the existing role with your new role data.

There’s a better solution than simply manually recreating your role inheritance in new environments: Sitecore Powershell Extensions.

I love SPE. In fact, my clients all know just what I’m talking about when I refer to “my girlfriend”. In this case, SPE lets us solve the problem using our old friend the CSV file.

I created a script that lets you get a bunch of roles using SPE’s Get-Role Filter option and then export all of the roles that those roles inherit into a CSV file (using the List-View window’s CSV export button). That script gets run on the source environment.

I then created another script that uses that exported CSV to create any missing roles and also update the role inheritance according to the exported values. You run this script on the target environment and feed it the CSV you just created.

This was all built for a specific use, but I’m sure I’ll need it again and odds are other Sitecore practitioners out there will too. Of course it couldn’t have been done without the wonderful tool that the SPE team has created. Thanks guys!!

About The Author