Skip to main content

Sitecore

Manage User’s Registry Settings After Login

Istock 948997140 (1)

Sitecore Registry is a place where Sitecore stores user data (i.e. data related to the user editing preferences and choices).   In some cases, setting a user registry to a default value each time the user logs into the CM would remove confusion and stress.   For example, if you set the checkbox raw values to unchecked every time the user logs into Sitecore then they don’t have to figure out how to set it back.   If this is something you know a client would want, then this post is for you!

What is user registry?

Read more about it here

What user registry settings are you changing?

Here is the list of user registry keys and values that the code will be modifying each time the user logs into Sitecore.  Be very careful that you assign the correct value to the key in order to avoid errors. 

  • Show Raw Values (setting it to false)
    • /Current_User/UserOptions.ContentEditor.ShowRawValues
    • true = show the raw values in Content Editor
    • false = don’t show the raw values in Content Editor
  • Show Hidden Items (setting it to true)
    • /Current_User/UserOptions.View.ShowHiddenItems
    • true = show the hidden items in Content Editor
    • false = don’t show the hidden items in Content Editor
  • Show Bucket Items (setting it to true)
    • /Current_User/UserOptions.View.ShowBucketItems
    • true = show the bucket items in Content Editor
    • false = don’t show the bucket items in Content Editor
  • Show Quick Info (setting it to true)
    • /Current_User/UserOptions.ContentEditor.ShowQuickInfo
    • true = show Quick Info area in Content Editor
    • false = don’t show Quick Info area in Content Editor
  • Show Entire Tree (setting it to true)
    • /Current_User/UserOptions.View.ShowEntireTree
    • true = show Entire Tree in Content Editor
    • false = don’t show Entire Tree in Content Editor
  • Smart Publish (setting it to true)
    • /Current_User/Publish/SmartPublish
    • true = select Smart Publish
    • false = unselect Smart Publish
  • Edit All Versions (setting it to on)
    • /Current_User/Page Editor/Show/EditAllVersions
    • on = set layout to Shared in Experience Editor
    • off = set layout to Final in Experience Editor
  • Open Rendering Properties Window after adding it in Experience Editor (setting it to true)
    • /Current_User/SelectRendering/IsOpenPropertiesChecked
    • true = open the window
    • false = don’t open the window
  • Increase/Decrease Workbox Page Size (setting it to 20)
    • /Current_User/Workbox/Page Size
    • any number = number will decide how many items in the Workbox will be shown per page

Where is the code and how does it work?

Code has been tested in Sitecore 9.1.1.   The code and patch file below will create a new processor under the pipeline “owin.cookieAuthentication.signedIn”.

  • Retrieve User data by Context or by Claims Identity
  • User has to be authenticated in order for the setting to be applied and saved to the User Profile
  • SiteContextSwitcher is used to switch the site context from “login” to “shell” which allows access to the correct site registry
  • Make sure to add the following dll to your project
    • Sitecore.Owin.Authentication.dll
    • Microsoft.Owin.Security.Cookies.dll
using Sitecore;
using Sitecore.Caching;
using Sitecore.Sites;
using System.Collections.Specialized;
using Sitecore.Security.Accounts;
using Sitecore.Diagnostics;
using Sitecore.Owin.Authentication.Pipelines.CookieAuthentication.SignedIn;
using System.Security.Claims;

namespace MySite.Custom.Pipelines.CookieAuthentication.SignedIn
{
    public class SetUserRegistry : SignedInProcessor
    {
        public override void Process(SignedInArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            User user = GetUser(args);

            // Make sure that the user has been authenicated else an error will occur when you save the profile
            if (user != null && user.IsAuthenticated)
            {
                SiteContext shellSite = SiteContext.GetSite("shell");
                string registryKey, registryValue, cacheKey;

                for (int i = 0; i < registryUpdates.Count; i++)
                {
                    // Get the registry key and value
                    registryKey = registryUpdates.GetKey(i);
                    registryValue = registryUpdates.Get(i);

                    // Clean up the registry key and replace Current_User with the loggedin user's name
                    registryKey = StringUtil.Left(registryKey, 250);
                    registryKey = registryKey.Replace("Current_User", user.Name.ToLowerInvariant());

                    // Save the registry key into the User profile
                    user.Profile[registryKey] = registryValue;
                    user.Profile.Save();

                    // Key to access the registry cache
                    cacheKey = "registry_" + registryKey;

                    // Registry cache is set per Site
                    // At this point the Site is login so we need to switch the site to shell 
                    // in order to update the correct site registry
                    using (new SiteContextSwitcher(shellSite))
                    {
                        RegistryCache registryCache = CacheManager.GetRegistryCache(Context.Site);
                        if (registryCache != null)
                            registryCache.SetValue(cacheKey, registryValue);
                    }
                }
            }
        }

        // This will return the user based on identity
        // Do not use Context.User because that will only come back as anonymous
        public User GetUser(SignedInArgs args)
        {
            User tempUser = null;

            // Get the user from the claims identity
            ClaimsIdentity identity = args.Context.Identity;
            string userName = (identity != null) ? identity.Name : null;
            if (!string.IsNullOrEmpty(userName))
            {
                // True will make sure that the user is authenticated
                tempUser = User.FromName(userName, true);
            }

            return tempUser;
        }

        // Here is where you would enter all of the user registry items you want changed when a user logs into the site
        NameValueCollection registryUpdates = new NameValueCollection()
        {
            { "/Current_User/UserOptions.ContentEditor.ShowRawValues", "false" },
            { "/Current_User/UserOptions.View.ShowHiddenItems", "true" },
            { "/Current_User/UserOptions.View.ShowBucketItems", "true" },
            { "/Current_User/UserOptions.ContentEditor.ShowQuickInfo", "true" },
            { "/Current_User/UserOptions.View.ShowEntireTree", "true" },
            { "/Current_User/Publish/SmartPublish", "true" },
            { "/Current_User/Page Editor/Show/EditAllVersions", "on" },
            { "/Current_User/SelectRendering/IsOpenPropertiesChecked", "true" },
            { "/Current_User/Workbox/Page Size", "20" }
        };
    }
}

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
    <pipelines>
      <owin.cookieAuthentication.signedIn>
        <processor patch:after="processor[@type='Sitecore.Owin.Authentication.Pipelines.CookieAuthentication.SignedIn.CleanupUserProfile, Sitecore.Owin.Authentication']" type="Mysite.Custom.Pipelines.CookieAuthentication.SignedIn.SetUserRegistry, Mysite.Custom" />
      </owin.cookieAuthentication.signedIn>
    </pipelines>
  </sitecore>
</configuration>

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CJ Morgan

CJ became a certified Sitecore developer in 2015. In his spare time, he enjoys playing video games, basketball and racquetball.

More from this Author

Follow Us