I am new to SharePoint programming and had written the below code to access all groups and group users from a site collection including all subsites using web.BreakRoleInheritance(true, false); .

So that I can access the subites that has broken inheritance and holding unique permission . But I am facing the error

Cannot change permissions of root web

Can any one let me know what's wrong in my below code

DataTable datatable = new DataTable();
            DataColumn datacolumn1 = new DataColumn("Site_URL");
            DataColumn datacolumn2 = new DataColumn("Login_Email");
            DataColumn datacolumn3 = new DataColumn("Login_User");
            DataColumn datacolumn4 = new DataColumn("Site_Group");


                    using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                        foreach (SPWeb web in site.AllWebs)
                            foreach (SPGroup group in web.Groups)
                                web.BreakRoleInheritance(true, false);
                                DataRow newrow = datatable.NewRow();

                                foreach (SPUser user in web.SiteUsers)
                                    newrow["Login_User"] = user.LoginName;
                                    newrow["Site_Group"] = group.Name;
                                    newrow["Login_Email"] = user.Email;
                                    newrow["Site_URL"] = web.Url;

                            GridView1.DataSource = datatable;
            catch(Exception ee)

                Label1.Text = ee.Message;


1 Answer 1


First, Based on your code, you don't need to use web.BreakRoleInheritance(true, false);

Second, Adding web.BreakRoleInheritance(true, false); in the foreach of SPGroup is pointless!!!

Third, the above code will not achieve your goal (access the subsites that have broken inheritance and holding unique permission) it just loops for each site user and adds it to grid view and logically it's wrong because you are trying to loop for each group then get the same site users!!

Fourth, yes you can't break the inheritance of the root site because it's the parent site!!

  • I am not breaking the permission in above code . But I am trying to read all the groups for all the subsites
    – beginner
    Sep 22, 2017 at 11:35
  • so no need to use web.BreakRoleInheritance(true, false); Sep 22, 2017 at 11:41
  • Also no need to loop again for each user ! Sep 22, 2017 at 11:41

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.