Set SharePoint List Item Permissions

This is an example of how to set specific permissions to a SharePoint list item. It will remove a SharePoint group from a List Item and will also add another one to it.

Reference
Microsoft.Sharepoint (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.dll)

Code
// Assuming you already have an instance of the SharePoint list item you want to set permissions to... SPListItem listItem; // You want to do this in elevated privileges, because the current user may not have enough rights // to update the permissions on the item. SPSecurity.RunWithElevatedPrivileges(delegate {   using (SPWeb web = new SPSite(listItem.Web.Url).OpenWeb)    {        // You have to work with 'secureItem' now. Dont' refer to listItem any more, as it is in another,        // non-elevated context!        SPListItem secureItem = web.Lists[listItem.ParentList.Title].GetItemById(listItem.ID);        bool allowUnsafeUpdates = secureItem.Web.AllowUnsafeUpdates;        bool siteAllowUnsafeUpdates = secureItem.Web.Site.AllowUnsafeUpdates;        bool formDigestSettings = secureItem.Web.Site.WebApplication.FormDigestSettings.Enabled;        secureItem.Web.AllowUnsafeUpdates = true;        secureItem.Web.Site.AllowUnsafeUpdates = true;        secureItem.Web.Site.WebApplication.FormDigestSettings.Enabled = false;        // If the item inherits the permission, break the inheritance        if (!secureItem.HasUniqueRoleAssignments)        {            secureItem.BreakRoleInheritance(true); // We have to set those to 'true' again, as the previous call may have reset them! secureItem.Web.AllowUnsafeUpdates = true; secureItem.Web.Site.AllowUnsafeUpdates = true; }       // Remove a SharePoint group from the item secureItem.RoleAssignments.Remove(web.SiteGroups["Some SharePoint Group Name"]); // Add a SharePoint group to the item SPRoleAssignment assignment = web.RoleAssignments.GetAssignmentByPrincipal(web.SiteGroups["Another SharePoint Group Name"]); secureItem.RoleAssignments.Add(assignment); try {           secureItem.SystemUpdate; // Restore originals secureItem.Web.AllowUnsafeUpdates = allowUnsafeUpdates; secureItem.Web.Site.AllowUnsafeUpdates = siteAllowUnsafeUpdates; secureItem.Web.Site.WebApplication.FormDigestSettings.Enabled = formDigestSettings; }       catch { } } });