Friday, August 12, 2011

Listing a site's owners or users with full control

Code below will list down a site's owners or users with full control

//first try the code below, should work most of the time

//unless users/admins mess up with the default groups

try
{

SPGroup myOwnerGroup = myWeb.AssociatedOwnerGroup;
foreach (SPUser myOwner in myOwnerGroup.Users)
{

//do what needs to be done here

}
}

catch (Exception ex)
{
//just ignore & proceed with the fall back mechanism to get the owners
}

//if the code above didn't return any values then the code below is the fall back mechanism

SPSite mySite = new SPSite(<website url>);
SPWeb myWeb = mySite.OpenWeb();

try
{
foreach (SPUser myWebUser in myWeb.AllUsers)
{

if (myWeb.DoesUserHavePermissions(myWebUser.LoginName, SPBasePermissions.ManagePermissions))
{

//do what needs to be done here

}
}

}
catch (Exception ex)
{

//error processing here

}