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;
}