Monday, March 5, 2012

remove all permissions for a list item in sharepoint

 private static void RemoveAllPermissions(SPItemEventProperties properties)
        {
            #region NewCode
            SPSite siteColl = new SPSite(properties.Web.Url);
            SPWeb site = siteColl.OpenWeb();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
                {
                    using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
                    {
                        ElevatedSite.AllowUnsafeUpdates = true;
                        SPList omylist = ElevatedSite.Lists[properties.ListTitle];
                        //SPListItem myitem = ElevatedSite.GetListItem(properties.ListItem.Url);
                        SPListItem myitem = omylist.Items[properties.ListItem.UniqueId];
                        //The below function Breaks the role assignment inheritance for the list and gives the current list its own copy of the role assignments
                        myitem.BreakRoleInheritance(true);
                        //Get the list of Role Assignments to list item and remove one by one.
                        SPRoleAssignmentCollection SPRoleAssColn = myitem.RoleAssignments;
                        for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
                        {
                            SPRoleAssColn.Remove(i);
                        }
                    }
                }
            });
            #endregion

        }

No comments: