0

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");

            datatable.Columns.Add(datacolumn1);
            datatable.Columns.Add(datacolumn2);
            datatable.Columns.Add(datacolumn3);
            datatable.Columns.Add(datacolumn4);

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    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;
                                    datatable.Rows.Add(newrow);
                                }

                            }
                            GridView1.DataSource = datatable;
                            GridView1.DataBind();
                        }
                    }
                });
            }
            catch(Exception ee)
            {

                Label1.Text = ee.Message;

            }

1 Answer 1

2

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!!

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