Wednesday, January 14, 2009

Getting site administrator(s)

Scenario:
I needed to find out atleast one user who is site admin , for purpose of creating a SPUserGroup.

Also for someother requirement I wanted to find out the list of users who are site admins.

Solution:
I love object model , I am sure you can sense that ;-)

Code:

     private SPUser GetSiteAdmin(SPWeb web) 
{
foreach (SPUser user in web.SiteUsers)
if (user.IsSiteAdmin)
return user;

return null;
}

private SPUserCollection GetSiteAdmins(SPWeb web)
{
SPUserCollection users = null;

foreach (SPUser user in web.SiteUsers)
if (user.IsSiteAdmin)
users.Add(user.LoginName, user.Email, user.Name, user.Notes);

return users;
}

0 comments: