Valid for Sitecore
5.3
Clearing security on an object and granting read and write permissions to a specific user
Sitecore versions: tested with 5.3.0.
The Problem:
Our code needs to clear security on an object, then grant read and write permissions to a specific user.
1.
The Solution
Use the SecurityField.SetRights method. It replaces the existing security settings with its parameters.
// get the Item to set rights on (/home Item in our case)
Database db = Sitecore.Configuration.Factory.GetDatabase("master");
Item itm = db.Items["/sitecore/content/home"];
// set the domain
Domain domain = Factory.GetDomain("sitecore");
// our code will assign Read and Write permissions
ItemRights rights;
rights = ItemRights.AllowRead | ItemRights.AllowWrite;
UserItem userItem = domain.GetUser("editor");
using (new SecurityDisabler())
{
// set permissions: editor can read and write "/sitecore/content/home"
itm.Editing.BeginEdit()
itm.SecurityField.SetRights(...
itm.Editing.EndEdit();
}