locked
IsSiteAdmin always returns True RRS feed

  • Question

  • Hello,

    I have the following code in the Page_Load event handler of my master page:

    SPUser user = SPContext.Current.Web.CurrentUser;
    bool isAdmin = user.IsSiteAdmin;

    For some odd reason the isAdmin variable is always True for all users although there is only one site collection administrator set up.

    Has anybody experienced similar behaviour?

    Thanks,
    Leszek

    Tuesday, July 19, 2011 2:30 PM

Answers

  • For now I've created my own function IsUserAdmin. It works perfectly fine:

     

    // Check if the current user is a site collection admin.
    bool

    isAdmin = IsUserAdmin(SPContext.Current.Web.CurrentUser);

     

    private bool IsUserAdmin(SPUser user)
    {
       
    bool isAdmin = false;
       
    SPUserCollection admins = SPContext.Current.Web.SiteAdministrators;

       
    foreach (SPUser admin in admins)
        {
           
    if(user.LoginName == admin.LoginName)
            {
                isAdmin =
    true;
                
    break;
            }
        }

        return isAdmin;
    }

    By the way, does anybody know how to re-write this function using SharePoint to LINQ? It would look much more elegant.

    Thanks,
    Leszek

     

     
    • Marked as answer by Shimin Huang Friday, July 29, 2011 3:54 AM
    Tuesday, July 19, 2011 6:14 PM

All replies

  • Try to delete any user from the site collection and add one more time with contribute rights and then check !!!
    Rahul Sharma -------------------------- Coding is all about passion !!!!!!
    Tuesday, July 19, 2011 2:47 PM
  • Thanks Rahul for your suggestion.

    I did exactly what you described: I deleted a user, re-created him, and granted contribute rights. Regardless of that the user's IsSiteAdmin property is always set to True.

    Any other suggestions?
    Leszek

     

    Tuesday, July 19, 2011 3:06 PM
  • Does the user get any other rights from other security groups in SharePoint or AD?  Also can you confirm you logged in as the user and the API returns the SPUser correctly?


    My SharePoint Blog - http://www.davehunter.co.uk/blog Twitter - @davehunter
    Tuesday, July 19, 2011 3:38 PM
  • Hi Ata,

    Make sure that you are not running your code under SPSecurity.RunWithElevatedPrivileges block.

     

     


    Ashraf ul Islam
    Sharepoint Consultant
    Tuesday, July 19, 2011 4:26 PM
  • Thanks Dave and Ashraf,

    Answering your questions:

    1. Does the user get any other rights from other security groups in SharePoint or AD?
    No. The only rights are granted indirectly through a group that have Read (and now also Contribute) permissions.

    2. Can you confirm you logged in as the user and the API returns the SPUser correctly?
    API returns a proper username (SPContext.Current.Web.CurrentUser.Name)

    3. Make sure that you are not running your code under SPSecurity.RunWithElevatedPrivileges block.
    I do not use the SPSecurity.RunWithElevatedPrivileges block.

    Any more suggestions?

    Thanks,
    Leszek

     

     

    Tuesday, July 19, 2011 5:34 PM
  • For now I've created my own function IsUserAdmin. It works perfectly fine:

     

    // Check if the current user is a site collection admin.
    bool

    isAdmin = IsUserAdmin(SPContext.Current.Web.CurrentUser);

     

    private bool IsUserAdmin(SPUser user)
    {
       
    bool isAdmin = false;
       
    SPUserCollection admins = SPContext.Current.Web.SiteAdministrators;

       
    foreach (SPUser admin in admins)
        {
           
    if(user.LoginName == admin.LoginName)
            {
                isAdmin =
    true;
                
    break;
            }
        }

        return isAdmin;
    }

    By the way, does anybody know how to re-write this function using SharePoint to LINQ? It would look much more elegant.

    Thanks,
    Leszek

     

     
    • Marked as answer by Shimin Huang Friday, July 29, 2011 3:54 AM
    Tuesday, July 19, 2011 6:14 PM
  • Please find LINQ version of the same

     

    private bool IsUserAdmin(SPUser user)
    {
        SPUserCollection admins = SPContext.Current.Web.SiteAdministrators;
        var adminUser = admins.Cast<SPUser>().Where(admin => admin.LoginName == user.LoginName).FirstOrDefault();
        return adminUser != null ? true : false;
    }
    

    Tuesday, August 9, 2011 2:40 PM
  • Thanks Balaji!

    Your LINQ version works like a charm :)

    Monday, August 29, 2011 5:21 PM
  • Welcome dude :)
    Wednesday, September 14, 2011 12:34 PM