2

I have certain page in SharePoint where i need to break inheritance remove certain security groups form the permissions permanently.Al i want to do is through programatically to be done.Please suggest any ideas on this. Thanks.

1 Answer 1

3

You can follow this post:
Assigning SharePoint List Item level permission programmatically

Excerpt from article:

RemoveAllPermissions Function

private static void RemoveAllPermissions(SPListItem CurrentlistItem)
    {
        //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--)
        {
            SPRoleAssColn.Remove(i);
        }
    }

GrantPermission Function

private static void GrantPermission(SPListItem CurrentListItem, SPWeb oSPWeb, SPRoleType SPRoleType, SPPrincipal SPPrincipal)
    {
        try
        {
            //Create one Role Definition i.e Full Controls, Contribute rights or Read rights etc.
            SPRoleDefinition oSPRoleDefinition = oSPWeb.RoleDefinitions.GetByType(SPRoleType);
            //Create one Role Assignment for the specified SP user or group.
            SPRoleAssignment oSPRoleAssignment = new SPRoleAssignment(SPPrincipal);
            //Bind the role definition to the role assignment object created for the user or group.
            oSPRoleAssignment.RoleDefinitionBindings.Add(oSPRoleDefinition);
            //Add it to the specified list item.
            CurrentListItem.RoleAssignments.Add(oSPRoleAssignment);
            //update the list item so that specified user assignment will have the access.
            CurrentListItem.Update();
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry("Error in UAR Initiation Workflow", "GrantPermission() : " + ex.Message);
        }
    }

You can get the List Item for SPFile using SPFile.Item

2
  • HI Adam i want to break permissions for page not list.Can you please suggest.
    – SPLearner
    Commented Sep 26, 2013 at 17:23
  • Yes Page is actually an item of Document Library.. you can get the page list item by SPFile.Item property ! Commented Sep 27, 2013 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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