Reset Sitecore item security settings from items template standard values
As you know, you can reset an item's presentation settings from the menu "Presentation/Reset" in the content editor. Consider a situation where there are tons of items and sub-items, and you suddenly realize that you will need to configure some item level security settings. It would have been nice, if similar to presentation settings, there was a "reset" option for security settings as well, so that you only need to specify the needed security settings at the template standard values level and then propagate the configuration to the tons of items/sub-items. I am going to discuss that today.
First the easy way, for those who just wish to install and proceed:
1- Install this Sitecore module package "ResetSecurity.zip" built using Sitecore.NET 9.2.0 (rev. 002893).
2- Once installed, you can now configure the security settings at the template standard values level.
3- Select the item using the said template (step-2), then click on the ribbon option "Reset" inside group "Security", under menu option "Security".
4- Once the content item is refreshed, you should now able to verify that the security settings specified at the item's template standard values level is now copied over to the item (and its sub-items depending on what you specified for inheritance settings), using the security access viewer. Simple right?
So, now let us talk on how this was achieved.
1- Build a command class out of "Sitecore.Shell.Framework.Commands.Command".
2- Override the Execute function as under:
public override void Execute(CommandContext context)
{
if (context.Items != null && context.Items.Length != 0)
{
Sitecore.Data.Items.Item targetItem = context.Items\[0\];
if (targetItem == null) return;
Sitecore.Data.Items.Item stdVals = targetItem.Template.StandardValues;
bool hasStandardValues = stdVals != null;
if (hasStandardValues)
{
using (new Sitecore.SecurityModel.SecurityDisabler())
{
targetItem.Editing.BeginEdit();
targetItem\["\_\_Security"\] = stdVals\["\_\_Security"\];
targetItem.Editing.EndEdit();
}
}
}
}
3- Notice that I am simply copying the raw values from the template standard values.
4- Create a command config file like following:
\[class path\] and \[class assembly\] should be as created in step-1.
5- Open the sitecore content editor for core database.
6- Go to '/sitecore/content/Applications/Content Editor/Ribbons/Chunks/Security'.
7- Make a duplicate item out of any of the child items and name it as "Reset" and specify values as below:
8- Notice the value specified for the "Click" field, which is same as what we mentioned for the command name in config file (step-4).
And that is it. You are all set.