Restrict access to create component datasources in Sitecore's Experience Editor

One of the many great features of Sitecore is the ability to give access to edit the content of your website to a wide range of content editors. Further, you can allow specific groups of editors to edit content in specific areas or based on specific templates.As you get deeper into the customization of access rights, however, you find that there are some common cases that are not covered by the standard security model that Sitecore exposes. For this, we need to add a customization. One such example is the topic of this post.

Requirements

  1. Restrict the creation of certain kinds of components in Experience Editor
  2. Which components are restricted should be customizable

How-To

Actually making this customization is relatively straight-forward, though you will need to touch a few places.

Update the SelectRenderingDatasource.xml Dialog

You will need to open the \Website\sitecore\shell\Applications\Dialogs\SelectRenderingDatasource\SelectRenderingDatasource.xml file and change the CodeBeside element's Type attribute to a custom type that executes some additional checking to limit the functionality of the dialog.

Create Custom SelectRenderingDatasourceForm

Create your custom SelectRenderingDataSourceForm and override the OnLoad function to disable the Create option based on your custom conditions:

using System;using Sitecore;using Sitecore.Buckets.Forms;using Sitecore.Data.Items;using Sitecore.Resources;using Sitecore.Security.Accounts;using Sitecore.Web.UI;namespace Custom.Sites.Web.sitecore.shell.Applications.Dialogs.SelectRenderingDatasource{	public class CustomSelectRenderingDatasourceForm : SelectRenderingDatasourceForm	{		protected override void OnLoad(EventArgs e)		{			base.OnLoad(e);			Item templateItem = this.SelectDatasourceOptions.DatasourcePrototype;			Item settingsItem = templateItem?.Database?.GetItem("/sitecore/system/Settings/Custom/Experience Editor Settings");			if (settingsItem != null)			{				if (settingsItem["Restricted Components"].Contains(templateItem.ID.ToString()))				{					DisableCreateOption();				}			}		}		/// <summary>		/// Decompiled from Sitecore.Shell.Applications.Dialogs.SelectRenderingDatasource.SelectRenderingDatasourceForm		/// </summary>		private void DisableCreateOption()		{			this.CreateOption.Disabled = true;			this.CreateOption.Class = "option-disabled";			this.CreateOption.Click = "javascript:void(0);";			if (this.CreateIcon?.Src != null)			{				this.CreateIcon.Src = Images.GetThemedImageSource(this.CreateIcon.Src, ImageDimension.id32x32, true);			}		}	}}

A couple things to note about the above code:

  1. This checks a configurable Sitecore item that is stored at /sitecore/system/Settings/Custom/Experience Editor Settings, but could be adjusted to look up an item by ID, use a config file or setting, or do any number of other checks
  2. The DisableCreateOption() method is decompiled from Sitecore.Shell.Applications.Dialogs.SelectRenderingDatasource.SelectRenderingDatasourceForm because it is private and therefore cannot be called from a subclass. We've made no modifications to its functionality, merely reproduced it in this class for accessibility.

Create Experience Editor Settings Item

Now you'll need to create the item which stores the list of component datasource templates that are restricted.

Create the template

I recommend the following template:Name: Experience Editor Settings

Fields

  • Restricted Components
    • Type: TreelistEx
    • Source: DataSource=/sitecore/templates/User Defined&IncludeTemplatesForDisplay=Template Folder,Template&IncludeTemplatesForSelection=Template

Create the item

  1. Under the /sitecore/system/Settings item, create a folder called Custom
  2. Inside the Custom folder, create an item from your Experience Editor Settings template
  3. Select the templates of the component datasources of which you'd like to restrict creation access
  4. Save your changes

Result

After building and deploying your code changes, you should now be able to see your updated dialog with the creation option disabled for the templates you selected in your Experience Editor Settings item: