Valid for Sitecore
5.3
Clearing the Security field of an item
The Problem:
Our code needs to clear the security rights that are set for a particular item.
1.
The Solution
Use the secField.Assignments.Remove method. The domain will be set to sitecore if the code is running under the context of the shell and extranet if the code is running on the front-end website.
using Sitecore;
using Sitecore.Data.Items;
using Sitecore.Data.Fields;
using Sitecore.SecurityModel;
Item contextItem = Sitecore.Context.Item;
// create instances of the Sitecore fields
if (contextItem != null)
{
if (contextItem.Fields[FieldIDs.Security] != null)
{
// getting the instance of the "SecurityField" of the context item
SecurityField secField = contextItem.Fields[FieldIDs.Security];
// check if the field is not empty already
if (!secField.IsEmpty)
{
// use the SecurityDisabler or login as a user with "Administrator" permissions on the context item
using (new Sitecore.SecurityModel.SecurityDisabler())
{
// entering the Editing mode
contextItem.Editing.BeginEdit();
// clearing the security assignments of the context item in the context domain
// the domain will be equaled to "sitecore" if the code is running under the context of the "shell"
// and "extranet" if the code is running on the front-end - "website"
secField.Assignments.Remove(Sitecore.Context.Domain.Name);
contextItem.Editing.EndEdit();
}
}
}
}