I have written code for removing all roleassignments for a splistitem using the itemadded() and itemupdated() eventhandler . But my requirement is how can I prevent the removal of currentloggedin user instead of removing everyone ?

So, I just DONT want to remove the current logged in user from the item's RoleAssignemnts. How to achieve this?

if (!CurrentListItem.HasUniqueRoleAssignments)
                CurrentWeb.AllowUnsafeUpdates = true;

                SPRoleAssignmentCollection SPRoleAssColn = CurrentListItem.RoleAssignments;
                for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
                    SPRoleAssignment roleAssignmentSingle = SPRoleAssColn[i];
                    System.Type t = roleAssignmentSingle.Member.GetType();
                    if (t.Name == "SPGroup" || t.Name == "SPUser")

You could try to add the following before your if (if (t.Name == "SPGroup" || t.Name == "SPUser")) statement:

if (roleAssignmentSingle.Member.LoginName == properties.UserLoginName)

Try the following:

if (roleAssignmentSingle.Member.ID != SPContext.Current.Web.CurrentUser.ID)
  • 2
    The problem with this approach is that SPContext.Current is null in event receivers. – Damjan Tomic Feb 4 '17 at 16:30
  • @DamjanTomic, if i use continue keyword along with your code, will my code OMIT the current user's permissions and remove ALL OTHER ROLE ASSIGNMENTS? this is what i need, i mean, i SHOULD NOT remove the roleassignments of currentlogged in user and SHOULD REMOVE remaining users or spgruops. – userAZLogicApps Feb 4 '17 at 17:00
  • @SaMolPP: Yes, that's right. – Damjan Tomic Feb 4 '17 at 19:25
  • thnx for the reply. Now,if i want to apply a custom permission for the current list item for current logged user "mycustomNoDeletePermission" , how to achieve this ? Currently , the current logged in user has Full Control on his document.Now, i wanna change this "FullControl" to "mycustomNoDeletePermission" , for the currentlistitem. – userAZLogicApps Feb 5 '17 at 2:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.