Monday, August 5, 2013

C# code to reset permissions of webpart page in sharepoint



Here is the small peace of code to reset the permissions of a sharepoint page.

Following are the steps i am doing here.

1. Breaking the role inheritance
2. Remove all the permissions of the file
3. Giving access to only the site owners group

public void ResetASPXPagePermissions(SPFile file, SPWeb web)
        {

            file.Item.BreakRoleInheritance(true);

            SPRoleAssignmentCollection SPRoleAssColn = file.Item.RoleAssignments;
            for (int i = SPRoleAssColn.Count - 1; i >= 0; i--)
            {
                SPRoleAssColn.Remove(i);
            }
            SPGroup grp = web.AssociatedOwnerGroup;
            if (grp != null)
            {
                SPRoleAssignment Assignment = web.RoleAssignments.GetAssignmentByPrincipal((SPPrincipal)grp);
                file.Item.RoleAssignments.Add(Assignment);
                file.Item.Update();
            }
            
        }

Thank you !!



No comments:

Post a Comment