Mark DiGiovanni

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
Determine User Site Group (Role) Membership in SharePoint 2003

Since there is no method to test whether a user is a member of a specific SharePoint Site Group such as Reader, Member, etc., use the following method:

        private bool IsCurrentUserInRole(string role)
        {
            bool inRole= false;

            SPWeb rootWeb = SPControl.GetContextSite(Context).RootWeb;
            SPRole  spRole = rootWeb.Roles[role];
            SPUser currentUser = rootWeb.CurrentUser;
   
            foreach(SPUser roleUser in spRole.Users)
            {
                if(roleUser.ID.Equals(currentUser.ID))
                {
                    inRole= true;
                    break;
                }
            }

            return inRole;
        }

A similar loop will have to be used to remove a user from a role.  This avoids having to trap an exception that gets thrown when the user cannot be found in the role collection.

Update:  I found this post.  Take a look.

--Mark


Posted Wed, Feb 16 2005 12:04 PM by Mark DiGiovanni
Filed under:

[Advertisement]

Comments

Matt wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Thu, Mar 3 2005 2:52 AM
I have tried this several ways, and have the same problem with your function - when the code attempts to access the user.Roles I get the following error:
System.Object {System.UnauthorizedAccessException} System.Object
at Microsoft.SharePoint.Library.SPRequestInternalClass.GetUsersDataAsSafeArray(String bstrUrl, UInt32 dwUsersScope, String bstrValue, UInt32& pdwColCount, UInt32& pdwRowCount, Object& pvarDataSet)\r\n
at Microsoft.SharePoint.Library.a.a(String A_0, UInt32 A_1, String A_2, UInt32& A_3, UInt32& A_4, Object& A_5)\r\n
at Microsoft.SharePoint.SPUserCollection.a(Boolean A_0, String[] A_1)\r\n at Microsoft.SharePoint.SPUserCollection.b()\r\n
at Microsoft.SharePoint.SPUserCollection.Undirty()\r\n
at Microsoft.SharePoint.SPBaseCollection.System.Collections.IEnumerable.GetEnumerator()\r\n
at IsCurrentUserInRole(String role)

Presumably because the logged in user is not a site admin?
Mark wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Thu, Mar 3 2005 6:04 AM
Yes. If the account accessing this collection does not have the appropriate rights, an error will be returned. This is a pain because there is no way to simply test to see if you can access the collection.


--Mark
Jon wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Wed, Apr 20 2005 7:58 AM
I get the same exception even when using impersonation as a sharepoint admin user. Please advise.
Mark DiGiovanni wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Wed, Apr 20 2005 10:42 AM
One way to do it is to wrap the test in a try/catch.

--Mark
Tsjaar wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Fri, Jun 3 2005 6:58 AM
How about these functions?
At least they determine if a user is admin.

SPWeb web = SPControl.GetContextWeb(Context);
if(web.UserIsWebAdmin)
{
//user is admin member of the current site
}

if(web.UserIsSiteAdmin)
{
//user is admin for the parent site
}

I should expect these functions on the User instead of the site, but it worked out for me. If a user is no admin, he cannot change special webpart settings.
sami assaf wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Mon, Mar 27 2006 5:30 AM
Hi man , i have the same problem did u resolve it , if yes can you please advise me at samiassaf@gmail.com
Lars Dahl wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Wed, Sep 6 2006 3:33 AM

Microsoft do have a comment about it: http://support.microsoft.com/?kbid=892866

BTW: Remember that a "SPUser" can be in a domaingroup, so check the "IsDomainGroup" property of the roleUser, becuase in that case you have to search through that domain group to see if the user is in.

Medes wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Mon, Oct 2 2006 2:14 PM

Hi, Guys

I have a web method (web service) that returns a SPList. when I build it (hit F5) it works but when i browse to it i get access denied exception. I have changed CenteralAdminAppPool identity and it uses MyDomain\Administrator and my web service application pool is CenteralAdminAppPool. but I get the exception when I use SPList.

thank you for your help

here is my mail kourosh.saleh@gmail.com

Kodi wrote re: Determine User Site Group (Role) Membership in SharePoint 2003
on Fri, Dec 1 2006 5:32 AM

When I create a new role (site-group)  in a sharepoint site, the users

with 'Reader' role are denied access to the site. If i delete the newly

created role, the users with 'Reader' role are able to access the

site. It has to be noted that i have custom webparts in my site. any

ideas?? Thanks in advance for the help.

Devlicio.us