LibrarySites.Banner

Working with User Options in the Sitecore Client

This blog post describes how to programmatically control user settings that apply in the Sitecore client. Examples of settings include Content Editor's customizable menus, desktop wallpaper, default publishing settings, and more.

As you might expect, Sitecore stores user information in the user's profile. And Sitecore, being an ASP.NET application, uses the ASP.NET membership tables to store this information. 

A couple of those profile values are easy to find. If you use the Sitecore User Manager and look at the Profile tab, you will see some profile values. 

The values that are displayed in the "Additional Properties" section are determined by the profile template that is assigned to the user. The default profile template is located in the core database at /sitecore/templates/System/Security/User.

If you want to read the value of one of those profile values, you can use the following code:

var wp = Sitecore.Context.User.Profile.GetCustomProperty("Wallpaper");

There is another value on this tab: Start Url. This value is not stored as a custom property. It is a built-in property. In order to read it, you can use the following code:

var su = Sitecore.Context.User.Profile.GetPropertyValue("StartUrl");

How did I know that "StartUrl" was the property name? You can get a list of property names using the following code:

foreach(System.Configuration.SettingsProperty prop in Sitecore.Security.UserProfile.Properties)
{
  var val = Sitecore.Context.User.Profile.GetPropertyValue(prop.Name);
}

If you run this code you will see there is a property named "SerializedData". This property is a NameValueCollection. If you check the values in the collection, you will notice it contains the custom properties. In other words, in the following code, the variables "wp1" and "wp2" contain the same value:

var wp1 = Sitecore.Context.User.Profile.GetCustomProperty("Wallpaper");
var profile = Sitecore.Context.User.Profile;
var data = profile.GetPropertyValue("SerializedData") as System.Collections.Specialized.NameValueCollection;
var wp2 = data["Wallpaper"];

In addition, if you look at the other available custom properties, you will find a variety of different properties that control how the Sitecore client works. If you want to list the available custom properties, you can use the following code:

foreach(var propName in Sitecore.Context.User.Profile.GetCustomPropertyNames())
{
  var propValue = Sitecore.Context.User.Profile.GetCustomProperty(propName);
}

This is where you find the user's publishing settings, the customized Content Editor menus, the links on the Sitecore desktop, and so on. The names of these properties is not as intuitive as "Wallpaper" and "StartUrl", but the properties are there.

Reading the values is one thing. Setting the values is another. You need to consider caching. The Sitecore API provides some classes that handle this for you. The static class Sitecore.Shell.UserOptions contains several of these classes. For example, if you want to disable the first page of wizards, you can use the following code:

Sitecore.Shell.UserOptions.Wizards.ShowFirstPage = false;

But you need to be careful. The UserOption classes use the site specified by Sitecore.Context.Site. If the site is not the "shell" site, the cache will not get cleared. The property will be updated, but the change will not be available until the cache is cleared. 

So to be safe, you should include code to clear the registry cache:

Sitecore.Shell.UserOptions.Wizards.ShowFirstPage = false;
var site = Sitecore.Sites.SiteContext.GetSite("shell");
var regCache = Sitecore.Caching.CacheManager.GetRegistryCache(site);
var key = "registry_/" + Sitecore.Context.GetUserName() + "/UserOptions.Wizards.ShowFirstPage";
regCache.RemoveKeysContaining(key);

I want to conclude with a list of some of the custom properties you may be interested in using:

Name: /[username]/UserOptions.ContentEditor.ShowRawValue
Description: Whether the "Raw Values" option is checked or not in Content Editor
Sample value: true

Name: /[username]/UserOptions.Wizards.ShowFirstPage
Description: Whether the first page of the wizard process should be displayed
Sample value: true

Name: /[username]/Desktop/Links
Description: The shortcuts that appear on the Sitecore desktop
Sample value: master^http%3a%2f%2fwin7%2ftest.aspx^test en^Applications%2f16x16%2fredo.png|master^%7b110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9%7d^Home+item en^Core%2f32x32%2fhome_brown.png|master^%7b3D6658D8-A0BF-4E75-B3E2-D050FABCF4E1%7d^Media+Library en^Applications%2f32x32%2fphoto_scenery.png

Name: /[username]/UserOptions.ContentEditor.VisibleStrips
Description: The strips that appear in Content Editor
Sample value: {C3FCEBB9-F38F-4AB4-857F-CFC415B6A342}|{D0FC99D9-5C65-43A3-ACB6-4495D44FB0E7}|{92ADE792-683F-480D-A30A-01C1F1BAAB53}|{F51BC51A-F5AF-41DF-AD11-F0D1B48FFC93}|{6553636F-E904-4F2D-B222-090D0A26298C}|{4CF64716-8B8C-4ABC-86C2-F9C5F3FF1128}|{D600C139-92D7-48DE-BB9F-4812A3DB9A8B}|{48967C19-C387-454D-8ED2-562DF1409D08}|{AED95E7E-874A-4CF0-924B-8DD90FC8D730}|{336E99C8-C45F-4CB7-9136-C9F8FE802E8F}|{BB2FBB55-EBC8-4724-9A1F-37BCFED0370D}|{1756C7EE-5F3A-4F62-83E4-586F0BE185D1}

Name: /[username]/Page Editor/Show/Treecrumb
Description: Whether the navigation bar is displayed in Page Editor
Sample value: false

Name: /[username]/Page Editor/Capability/edit
Description: Whether content editing mode is enabled in Page Editor
Sample value: off

Name: /[username]/Capability/design
Description: Whether design mode is enabled in Page Editor
Sample value: on

Name: Packager/File
Description: Name of the last package that was imported
Sample value: mycomponents-1.0.zip

Name: WallpaperBackground
Description: Color for the Sitecore Desktop
Sample value: #07337C

Name: /[username]/Publish/IncrementalPublish
Description: Whether the incremental publish option is selected by default in the publish dialog
Sample value: true

Name: /[username]/Publish/Languages
Description: The languages that are selected by default in the publish dialog
Sample value: da|de-DE|en

Name: /[username]/Publish/PublishChildren
Description: Whether the publish children option is selected by default in the publish dialog
Sample value: true

Name: /[username]/Publish/Republish
Description: Whether the republish option is selected by default in the publish dialog
Sample value: false

Name: /[username]/Publish/SmartPublish
Description: Whether the smartpublish option is selected by default in the publish dialog
Sample value: false

Name: /[username]/Publish/Targets
Description: Which publishing targets should be selected by default in the publish dialog
Sample value: {8E080626-DDC3-4EF4-A1D1-F0BE4A200254}

Let me know how you are using these values to customize the Sitecore client!

  • Hi,

    would you happen to know if caching is involved when doing Context.User.Profile.GetCustomProperty([string]) ?

    In an environment of two Sitecore instances on separate IIS'es, sharing the same Sitecore database, I found that when setting a property in the one instance I can not read it from the other instance, indicating that maybe the Context.User.Profile.GetCustomProperty() does not read from the shared database on the second instance.

    Any suggestions?