locked
Check if a user is site collection administrator RRS feed

  • Question

  • Hello,

    I'm writing a tool in VS2010/C# for SharePoint 2010 where I need to check if a provided user login is a site collection administrator in SharePoint.

    I found several pages in Google that describe this by fetching the SPUserCollection on the rootweb of the site collection, but this doesn't seem to work if you only have added the user as site collection administrator, and not somewhere else in SharePoint.
    Then this user is not returned in the users collection object, even though this user has complete access as site collection administrator.

    How can I check if a user is a site collection administrator, when this user is not given specific access on the sites level in SharePoint ?

    Friday, May 14, 2010 1:24 PM

Answers

  • Well there are some other properties you can use to iterate, i cant test myself at the moment:

    SPWeb.AllUsers

    SPWeb.SiteUsers

    SPWeb.SiteAdministrators


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    • Proposed as answer by Stuart Starrs Friday, May 14, 2010 2:28 PM
    • Marked as answer by kurtvd Friday, May 14, 2010 2:29 PM
    Friday, May 14, 2010 1:48 PM
  • Have you tried using the SiteAdministrators collection of the SPWeb. If the user has been made a site collection administrator he will automatically show up in the SiteAdministrators collection of all subsites.
    certdev.com
    • Proposed as answer by Stuart Starrs Friday, May 14, 2010 2:28 PM
    • Marked as answer by kurtvd Friday, May 14, 2010 2:29 PM
    Friday, May 14, 2010 2:06 PM

All replies

  • SPContext.Current.Site.RootWeb.CurrentUser.IsSiteAdmin - is the quickest way to this, but basically it is a property on the SPUser object

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spuser.issiteadmin.aspx


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    • Proposed as answer by Nik Patel Friday, February 17, 2012 6:12 PM
    Friday, May 14, 2010 1:27 PM
  • If you only added the user in the site collection administrators, the rootweb will not return this user.
    I can't use CurrentUser, as I will be providing a login in the tool to check permissions from another user.

    What I have now:

    SPWeb MainWeb = site.OpenWeb();
    SPUserCollection users = MainWeb.Users;
    foreach (SPUser user in users)
    {
        if (user.LoginName.ToLower() == txtLogin.Text.ToLower() && user.IsSiteAdmin)
        {
            //user is a site collection administrator
        }
    }

    But this only works if the user has been added as a regular user in one of the (sub)sites of the site collection, in a sharepoint group or as an individual. If that is not the case, the user is not returned in the MainWeb.Users collection.

    Friday, May 14, 2010 1:34 PM
  • To visualise it more:

    I have 2 site collection administrators defined:

    Image and video hosting by TinyPic

    Kurt is the user that was added when creating the site collection, this user is automatically added to members, owners groups etc as well.
    TestUser is a user that I added afterwards, I only added this user in the site collection administrators as shown above, I have not added this user in any SharePoint group or with individual permissions in SharePoint. So the only place where I will see this user is in the screenshot above.

    Then when running my code, I added a breakpoint on the "SPUserCollection users = MainWeb.Users".
    When checking the XML contents of the "users" object, this is what I get:

    Image and video hosting by TinyPic

    No sign of TestUser, only kurt is returned (because that user also exists in SharePoint groups within the site collection).
    So, how can I check if TestUser is a site collection administrator ?

    Friday, May 14, 2010 1:47 PM
  • Well there are some other properties you can use to iterate, i cant test myself at the moment:

    SPWeb.AllUsers

    SPWeb.SiteUsers

    SPWeb.SiteAdministrators


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    • Proposed as answer by Stuart Starrs Friday, May 14, 2010 2:28 PM
    • Marked as answer by kurtvd Friday, May 14, 2010 2:29 PM
    Friday, May 14, 2010 1:48 PM
  • Have you tried using the SiteAdministrators collection of the SPWeb. If the user has been made a site collection administrator he will automatically show up in the SiteAdministrators collection of all subsites.
    certdev.com
    • Proposed as answer by Stuart Starrs Friday, May 14, 2010 2:28 PM
    • Marked as answer by kurtvd Friday, May 14, 2010 2:29 PM
    Friday, May 14, 2010 2:06 PM
  • Hi!

    Here is the solution to your problem....

    Users will be added to the rootweb.users the first time they access it (or a subsite) if an active directory group allow them to enter....

    You dont want to wait until then to do your check... so... the method is quite simple...

    Just use:

    Microsoft.SharePoint.SPUser user = site.RootWeb.EnsureUser(strLogin);

    This will get the user by its login name and add it to rootweb.users if he is still not there.... Afterwards you can do your check...

    if(user.IsSiteAdmin)
     //do whatever you want...
    
    

    Hope this help!

    Friday, May 14, 2010 2:21 PM
  • Thank you Steve and Stuart, I was not aware of the SiteAdministrators property.
    When using that one, I do get the complete list of users that are site collection.

    Thanks!
    Friday, May 14, 2010 2:21 PM
  • Why are you ignoring my reply :/
    Friday, May 14, 2010 2:38 PM
  • Raevean, your reply is actually slightly incorrect. When a user accesses a site and they are granted such access due to their group membership they are added to the SPWeb. AllUsers property, not the SPWeb.Users.

    Also, your method would only be relevant if kurtvd actually wanted all the users to be added as explicit users before finding out if they are a site collection administrator - there are better ways of doing this, which were in my and Steve's response.

    Dont give up though! It all helps!


    Read my wiki at www.intheknow.it for more code and tips for developing with SharePoint 2007 & 2010
    Twitter: @starznet

    Technical Architect at Starznet Ltd. WSS/MOSS development and customisation with a primary focus on CMS.
    Friday, May 14, 2010 2:59 PM
  • After some testing... User is added to SiteUsers (not AllUsers neither than Users) collection when EnsureUser is called... Same behavior at first access to the site....

     

    Friday, May 14, 2010 3:27 PM
  • Raevan,

    The method of Steven and Stuart was the one I was looking for and which works best for my needs.
    I didn't ignore your reply, but your solution would be too complex for what I need. By checking the property SiteAdministrators, I can access the users that are site collection administrator directly, so there is no need to work with AllUsers, Users, or SiteUsers in my case.

    Thanks though for your reply and the effort in trying to help!

    Kurt

    Monday, May 17, 2010 8:18 AM