Recently, I had to serialize¬†newly created users and roles, fortunately, Unicorn serializes roles/users efficiently ūüôā

The new guy

First of all, let's create a new user in User Manager and a role using Role Manager(I assigned my new role to the new user).

New user which has assigned my

New user which has assigned my "NewRole"

Configuration

Now when you have the user and the role, you have to install the following Unicorn nuget packages:

  • Unicorn.Roles v4.0.2
  • Unicorn.User¬†v4.0.2
Unicorn nugets required

Unicorn nugets required

These nugets install configuration files under App_Config/Include/Unicorn which define Users and Roles synchronization. You can find examples here in Unicorn GitHub repo(Users, Roles). However, the most important are these four files:

  • Unicorn.Roles.config - configures Unicorn to sync Roles
  • Unicorn.Configs.Default.Roles.config.example - contains an example of roles synchronization
  • Unicorn.Users.config -¬†configures Unicorn to sync Roles
  • Unicorn.Configs.Default.Users.config.example - contains an example of users¬†synchronization

Pro tip: if you want to copy the config files from Unicorn GitHub you have to uncomment ""  line from both Unicorn.Configs.Default.Users.config.example and Unicorn.Configs.Default.Roles.config.example.

Unicorn.Roles.config and Unicorn.Users.config

These configs are very similar and they prepare Unicorn to sync roles and users. One important line is:   which specifies the physical path for your serialized items.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <unicorn>
      <defaults>
        <roleDataStore physicalRootPath="$(dataFolder)\Unicorn\Unicorn.Roles\$(configurationName)" type="Unicorn.Roles.Data.FilesystemRoleDataStore, Unicorn.Roles" singleInstance="true"/>
        <roleLoader type="Unicorn.Roles.Loader.RoleLoader, Unicorn.Roles" singleInstance="true" />
        <roleLoaderLogger type="Unicorn.Roles.Loader.DefaultRoleLoaderLogger, Unicorn.Roles" singleInstance="true" />

        <roleSerializationFormatter type="Unicorn.Roles.Formatting.YamlRoleSerializationFormatter, Unicorn.Roles" singleInstance="true" />

        <roleSyncConfiguration removeOrphans="true" type="Unicorn.Roles.Loader.DefaultRoleSyncConfiguration, Unicorn.Roles" singleInstance="true" />
      </defaults>
    </unicorn>

    <pipelines>
      <unicornSyncComplete>
        <processor type="Unicorn.Roles.Pipelines.UnicornSyncComplete.SyncRoles, Unicorn.Roles" />
      </unicornSyncComplete>
      <unicornReserializeComplete>
        <processor type="Unicorn.Roles.Pipelines.UnicornReserializeComplete.ReserializeRoles, Unicorn.Roles" />
      </unicornReserializeComplete>
    </pipelines>
  </sitecore>
</configuration>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <unicorn>
      <defaults>
        <userDataStore physicalRootPath="$(dataFolder)\Unicorn\Unicorn.Users\$(configurationName)" type="Unicorn.Users.Data.FilesystemUserDataStore, Unicorn.Users" singleInstance="true"/>
        <userLoader type="Unicorn.Users.Loader.UserLoader, Unicorn.Users" singleInstance="true" />
        <userLoaderLogger type="Unicorn.Users.Loader.DefaultUserLoaderLogger, Unicorn.Users" singleInstance="true" />

        <userSerializationFormatter type="Unicorn.Users.Formatting.YamlUserSerializationFormatter, Unicorn.Users" singleInstance="true" />

        <userSyncConfiguration removeOrphans="true" defaultPassword="random" minPasswordLength="8" type="Unicorn.Users.Loader.DefaultUserSyncConfiguration, Unicorn.Users" singleInstance="true" />
      </defaults>
    </unicorn>

    <pipelines>
      <unicornSyncComplete>
        <processor type="Unicorn.Users.Pipelines.UnicornSyncComplete.SyncUsers, Unicorn.Users" />
      </unicornSyncComplete>
      <unicornReserializeComplete>
        <processor type="Unicorn.Users.Pipelines.UnicornReserializeComplete.ReserializeUsers, Unicorn.Users" />
      </unicornReserializeComplete>
    </pipelines>
  </sitecore>
</configuration>

Roles.Serialization and Users.Serialization

These files are the two that I created using the example configuration files provided by Unicorn nugets. They contain synchronization rules for roles and users respectively.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <unicorn>
      <configurations>
        <configuration name="Roles.Serialization">
          <predicate type="Unicorn.Roles.Predicates.EmptyPredicate, Unicorn.Roles" singleInstance="true" />

          <rolePredicate type="Unicorn.Roles.RolePredicates.ConfigurationRolePredicate, Unicorn.Roles" singleInstance="true">
            <include domain="sitecore" pattern="NewRole" />
          </rolePredicate>

          <roleSyncConfiguration removeOrphans="false" type="Unicorn.Roles.Loader.DefaultRoleSyncConfiguration, Unicorn.Roles" singleInstance="true" />
        </configuration>
      </configurations>
    </unicorn>
  </sitecore>
</configuration>

 

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <unicorn>
      <configurations>
        <configuration name="Users.Serialization">
          <predicate type="Unicorn.Roles.Predicates.EmptyPredicate, Unicorn.Roles" singleInstance="true" />
          <userPredicate type="Unicorn.Users.UserPredicates.ConfigurationUserPredicate, Unicorn.Users" singleInstance="true">
            <include domain="sitecore" pattern="NewUser" />
          </userPredicate>
          <userSyncConfiguration removeOrphans="true" defaultPassword="random" minPasswordLength="8" type="Unicorn.Users.Loader.DefaultUserSyncConfiguration, Unicorn.Users" singleInstance="true" />
        </configuration>
      </configurations>
    </unicorn>
  </sitecore>
</configuration>

Serialize it

Now, you just have to build, deploy the config files and reserialize items in Unicorn control panel.

Serialize

Unicorn control panel result

Now, you can find your users and roles serialized on the disk.

Serialized new role and user

Serialized items

Hope this was helpful, and see you next time.

Subscribe to my newsletter:


Leave a Reply

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

Related Posts

meetup

SUGPL Meetup Warsaw ‚Äď Video Presentation

Video presentation SUGPL Meetup Warsaw – 23 March 2018 In my last post,¬†I have invited you all to a SUGPL Meetup Warsaw which took place on 23 March 2018. I have been one of the¬†presenters. Read more…

meetup

SUGPL Meetup Warsaw – 23 March 2018

SUGPL Meetup Warsaw This gonna be a short post but I would like to invite¬†you all to a SUGPL Meetup Warsaw which takes place on 23 March 2018. I will be there and I will Read more…

sitecore

Customize Rich Text Editor in Sitecore

Recently, I have come up with an idea to Customize Rich Text Editor in Sitecore by adding a button that embeds GIPHY player into my content. So, I thought that I will share this with Read more…