I am creating a workflow that removes permissions from a list item then adds a new set of permissions. I know how to add the permissions. but I was wondering with removing the permissions is there an easy way to remove all the permissions from the list, or do you have to go through every group and user and tell the workflow to remove them. this is sharepoint 2010
-
Is it SharePoint 2013 or SharePoint 2010 workflow infrastructure?– ECM4DAug 10, 2015 at 12:57
-
it is sharepoint 2010– jamesdAug 10, 2015 at 13:01
-
1Using impersonation step, there is a way to break inheritance and remove all the permissions. Check: spcycle.blogspot.in/2012/01/…. Specifically, replace permissions will do the job.– AkhoyAug 10, 2015 at 13:02
-
This workflow will trigger automatically or manually on button click?– Shivendra PatelAug 10, 2015 at 13:07
2 Answers
Adding comment as answer
Using impersonation step, there is a way to break inheritance and remove all the permissions. Check: Using item level permissions. Specifically, replace permissions
action will do the job.
If you have to customize the permissions on item level, while on added and updated events you have to remove all permissions (except for author), then grant permission for each user as you want to assign. Suppose, i am taking example of Assigned Users, Everyone and Author itself. Just go through the code here, you will get idea.
private static void GrantPermission(string EveryonePermission, SPListItem CurrentListItem, SPWeb oSPWeb, string[] assignedUsersArray)
{
var fullUserName = CurrentListItem[SPBuiltInFieldId.Author] as string;
var userName = fullUserName.Split('#')[1];
SPUser Author = oSPWeb.EnsureUser(userName);
SPUser currentUser = oSPWeb.CurrentUser;
RemoveAllPermissions(CurrentListItem, currentUser);
SPUser allUsers = oSPWeb.AllUsers[@"c:0(.s|true"]; // for everyone
SPRoleDefinitionCollection roleCollection = oSPWeb.RoleDefinitions;
SPRoleDefinition EveryOneDefinition = roleCollection[EveryonePermission]; // for everyone
SPRoleDefinition roleContribute = roleCollection["Full Control"];// for assigned user and for author
SPRoleAssignment oSPRoleAssignmentAuthor = new SPRoleAssignment(Author);// for author
SPRoleAssignment roleAssignment = new SPRoleAssignment((SPPrincipal)allUsers);// for everyone
if (assignedUsersArray.Length > 1)
{
for (int i = 0; i < assignedUsersArray.Length; i++)
{
if (assignedUsersArray[i] != null)
{
SPUser assignedUser = oSPWeb.EnsureUser(assignedUsersArray[i]);
SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(assignedUser);// for assigned user
oSPRoleAssignment.RoleDefinitionBindings.Add(roleContribute);// for assigned user
CurrentListItem.RoleAssignments.Add(oSPRoleAssignment);
}
}
}
oSPRoleAssignmentAuthor.RoleDefinitionBindings.Add(roleContribute);// for author
roleAssignment.RoleDefinitionBindings.Add(EveryOneDefinition);// for everyone
CurrentListItem.RoleAssignments.Add(oSPRoleAssignmentAuthor);
CurrentListItem.RoleAssignments.Add(roleAssignment);
CurrentListItem.Update();
}
private static void RemoveAllPermissions(SPListItem CurrentlistItem,SPUser currentUser)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//The below function Breaks the role assignment inheritance for the list and gives the current list its own copy of the role assignments
CurrentlistItem.BreakRoleInheritance(true);
//Get the list of Role Assignments to list item and remove one by one.
SPRoleAssignmentCollection SPRoleAssColn = CurrentlistItem.RoleAssignments;
for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
{
string str = SPRoleAssColn.ToString();
if (SPRoleAssColn[i].Member.ToString() == currentUser.ToString())
{
}
else
{
SPRoleAssColn.Remove(i);
}
}
});
}