0

I am currently working on creating a visual web part that displays a list of users with full control to a SharePoint site, along with their loginID and email. The code I've written lists all the users in the Owner Group (which means they have full control). But this does not fulfill my objective, because I just realized that users that aren't in the owner group can also have full control.

This is the code I have so far:

namespace DisplaySiteOwnersVWP.VisualWebPart1
{
[ToolboxItemAttribute(false)]
public partial class VisualWebPart1 : WebPart
{

    string mySiteURL = "my Site URL goes here";
    DataTable dtTable = new DataTable();

    public VisualWebPart1()
    {
    }



    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        InitializeControl();
    }



    protected void Page_Load(object sender, EventArgs e)
    {

        dtTable.Columns.Add(new DataColumn("Name"));
        dtTable.Columns.Add(new DataColumn("UserID"));
        dtTable.Columns.Add(new DataColumn("Email"));


        using (SPSite site = new SPSite(mySiteURL))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPGroup ownerGroup = web.AssociatedOwnerGroup;
                if (ownerGroup != null)
                {
                    foreach (SPUser user in ownerGroup.Users)
                    {

                            DataRow dtRow = dtTable.NewRow();
                            dtRow["Name"] = user.Name;
                            dtRow["UserID"] = user.LoginName.Substring(6);
                            dtRow["Email"] = user.Email;
                            dtTable.Rows.Add(dtRow);

                    }
                }

                gvOwners.DataSource = dtTable;
                gvOwners.DataBind();

            }
        }

    }


}
}

Can someone please help me with how to display all the users with full control? Instead of just displaying users in the Owner group.

Does this have to do with a property that has to do with site permissions?

1 Answer 1

0

You might need to use SPRoleDefinitionCollection

using(SPWeb web = SPSite(webUrl).OpenWeb())
{

    SPRoleDefinitionCollection roleDefinitions = web.RoleDefinitions;
    SPRoleDefinition roleDefinition = roleDefinitions["Full Control"];

    foreach (SPRoleAssignment roleAssigment in web.RoleAssignments)
    {
      if (roleAssigment.RoleDefinitionBindings.Contains(roleDefinition))
      {
          SPPrincipal principal = roleAssigment.Member;
      }

    }

}
1
  • Thanks. The problem with this is that it displays users on the site permissions list with full control, but it does not display users in the owner group with full control. For example I have someone named Anna. She has been given limited access on the site permissions list. But I added her to the owner group (which gives her full control). So the code you gave above does not end up listing her as having full control.
    – Mischa
    Jul 2, 2014 at 11:53

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.