Sharepoint 2010: How To Verify if a site user is site Administrator

In many Sharepoint projects the following code has been user to check if a certain user has full control or not over a specific site:

if (!SPContext.Current.Web.CurrentUser.IsSiteAdmin)

But this code only check if a user is a Site Collection administrator. Even if the current user is in the owners group, it will not work. And therefore, problems will happen once implemented live.

Workaround:

I wrote a method that checks if a user belongs to the Owners group:

if(!isCurrentUserOwner())

Method:

private bool isCurrentUserOwner()
        {
            bool currentUserOwner = false;

            SPUser currentUser = SPContext.Current.Web.CurrentUser;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    SPSite currentSite = new SPSite(SPContext.Current.Site.Url);
                    SPWeb currentWeb = currentSite.OpenWeb(SPContext.Current.Web.ID);
                    SPGroup ownersGroup = currentWeb.AssociatedOwnerGroup;

                    //Check if currentuser belongs to owners group of the current web
                    foreach(SPUser user in ownersGroup.Users)
                    {
                        if (currentUser.ID == user.ID)
                        {
                            currentUserOwner = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    //blah blah
                }              
            });

            //Not an owner member
            return currentUserOwner;
        }

About Gokul


I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

  • http://www.simplecodestuffs.com/sharepoint-2010-how-to-verify-if-a-site-user-is-site-administrator/ Ravikumar

    Nice article, it saved my time

  • James Whitmore

    I was looking at some of your content on this site and I think this web site is really instructive! Retain posting .

  • Lahir

    Nice . Thanks for your article