Programmatically breaking inheritance and adding unique permission SharePoint List

Programmatically breaking inheritance, adding unique permission SharePoint List

In this post I described you to Programmatically breaking inheritance, adding unique permission SharePoint List. When you create the group on SharePoint site, It’s automatically added to your SP List/ Document Library Permissions.Because the library inherits permissions from its parent. When you break inheritance then This library has unique permissions.

[AdSense-A]

using (SPSite site = new SPSite(SiteCollectionURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                   
                    SPList list = web.Lists["Document Library Name"];
                    list.BreakRoleInheritance(true);

                    
                    int Exist = web.Groups.OfType<SPGroup>().Count(g => g.Name.Equals("Group Name"));
                    if (Exist == 1)
                     {
                         SPGroup myGroup = web.SiteGroups["Group Name"];
                         SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
                         SPRoleAssignment roleAssignment = new SPRoleAssignment(myGroup);
                         SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
                         roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
                         roleAssignments.Add(roleAssignment);
                         list.RoleAssignments.Add(roleAssignment);
                         list.Update();
                     }

                }
            }

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!