Geeks With Blogs

News


INETA Community Speakers Program


Mike Huguet
Mike Huguet

A smörgåsbord of technology

View my profile on LinkedIn





Mike Huguet I'm just talking out loud

I found it very odd that this was not available when doing some search engine queries.  My requirement was to take in an AD user name and query SharePoint 2010 to determine the SharePoint groups in which the account belongs.  The code was to run from within a RIA Authentication Service, which is code run on a server and is not likely on the SharePoint server.  This code will also work with SharePoint 2007 (WSS 3.0 and MOSS 2007).  You will need to add a Web Reference to http(s)://<spservername>/_vti_bin/usergroup.asmx and name it SharePointUserGroupService.  Voila, you have your list of SharePoint groups for a user.  Maybe in the next version of the SharePoint Client Object Models (SCOM) this will be included.  It does required LINQ support (.NET 3.5 or greater) where this code is run (not necessarily on the server).

   1: private string[] GetRoles(string userName)
   2: {
   3:     UserGroup userGroupService = new UserGroup();
   4:     userGroupService.UseDefaultCredentials = true;
   5:     XmlNode xNode = userGroupService.GetGroupCollectionFromUser(userName);
   6:  
   7:     XElement groupXml = XElement.Parse(xNode.OuterXml);
   8:  
   9:     XNamespace ns = groupXml.Name.Namespace;
  10:  
  11:     var item = from xml in groupXml.Elements(ns + "Groups").Elements(ns + "Group")
  12:                let GroupName = (string)xml.Attribute("Name")
  13:                select GroupName; 
  14:  
  15:     return item.ToArray();
  16:  
  17: }
Posted on Tuesday, August 17, 2010 9:19 AM .NET General , SharePoint | Back to top


Comments on this post: Getting a List of a User’s SharePoint Groups

# re: Getting a List of a User’s SharePoint Groups
Requesting Gravatar...
I've tried to create this in VS2008 using SP2007, but it doesnt recognise the UserGroup constructor. It recognizes UserGroupSoap, but doesnt have the same methods....am I doing something wrong? Can you post all of the page, because I'm also getting assembly errors with the XML linq query.

Thanks,Satpal
Left by Satpal on Mar 09, 2012 10:46 AM

# re: Getting a List of a User’s SharePoint Groups
Requesting Gravatar...
You need to add a reference to the SharePoint dll in your project.
Left by Jovino on Apr 23, 2012 4:46 AM

# re: Getting a List of a User’s SharePoint Groups
Requesting Gravatar...
You need to add a Web Reference instead of a Service Reference as shown in http://msdn.microsoft.com/en-us/library/bb628649.aspx
Left by Luigi on Nov 08, 2012 8:22 AM

# re: Getting a List of a User’s SharePoint Groups
Requesting Gravatar...
Just wanted to let you know that this works with SharePoint 2013 as well!

Thanks for the code.
Left by SharePoint User on Sep 30, 2013 10:40 AM

Your comment:
 (will show your gravatar)


Copyright © Mike Huguet | Powered by: GeeksWithBlogs.net