Sharing SharePoint

Always a rookie in the SharePoint World

  • Follow me on Twitter

    Error: Twitter did not respond. Please wait a few minutes and refresh this page.

  • Blog Stats

    • 60,367 hits
  • Advertisements

Display site members from AD Groups

Posted by Jessica Wang on July 13, 2008

I recently have to do this for a client. The web part is suppose to display a list of all users from the Sharepoint Security groups for every site. The out of the box webpart(Site Users) will display only the name of the AD group and not the members within it.

To get started, it is easier if you installed the Webpart templates for VS.NET

Created a solution using webpart template and you are ready to go.

Call this function to pass in the name of the ADgroup and it will return a list of users name.

//Query Active Directory to get users from Active Directory Groups

public StringCollection GetGroupMembers(stringstrGroup)

{StringCollection groupMemebers = new StringCollection(); 


{DirectoryEntry ent = new DirectoryEntry(LDAP://OU=youOU,DC=yourDC);

DirectorySearcher srch = new DirectorySearcher(“(CN=” + strGroup + “)”);

SearchResultCollection coll = srch.FindAll(); 

foreach (SearchResult rs in coll)

{ResultPropertyCollection resultPropColl = rs.Properties;

foreach (Object memberColl in resultPropColl[“member”])

{DirectoryEntry gpMemberEntry = new DirectoryEntry(“LDAP://”+ memberColl);

System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;

//getting user properties from AD

object obVal = userProps[“displayName”].Value;

object obAcc = userProps[“sAMAccountName”].Value;

if (null != obVal) {

 groupMemebers.Add( “User Name:” +obAcc.ToString() + “, User login name:” + obVal.ToString() + “<br>”);}}}}

catch (Exception ex)


Return groupMemebers;

To get the names of the current site users from Sharepoint Security Groups:

ArrayList belongToDomain = new ArrayList();

ArrayList names = new ArrayList();

using(SPSite collSite = new SPSite(SPContext.Current.Site.ID))

{using (SPWeb elevatedWeb = collSite.OpenWeb(SPContext.Current.Web.ID))

{//All users in the site

SPUserCollection collUser = SPContext.Current.Web.AllUsers;

SPGroupCollection collgroup = SPContext.Current.Web.Groups;

//for each item in the collection of groups

foreach (object group in collgroup){

//display all users other then the visitors

if (group.ToString() != “Visitors”){

//check that the users in the whole site collection belongs to current site group

foreach (SPUser singleuser in collUser)

{//get the list of groups that the user belongs to

foreach (SPGroup userGroup in singleuser.Groups)

{//check if it matches any of the current site groups

if (group.ToString() == userGroup.ToString())

{//check if the user from the sharepoint group is a AD group

if (singleuser.IsDomainGroup)

{//pass the name into Array that query the AD



//otherwise add into the Array that stores list of names, in case the user name is not from an AD group.


);}} }}}}}}

Now that we have the names of the AD groups from the share point security groups and query the AD for a list of user name. It is now in the names array, we need to make sure that there are no duplicate names. So call the function below and pass in the names array.

//remove duplicate users name Function

public ArrayList RemoveDups(ArrayList items)


 ArrayList noDups = new ArrayList();

 foreach (string strItem in items)


        if (!noDups.Contains(strItem.Trim()))






    return noDups;}


10 Responses to “Display site members from AD Groups”

  1. jeosaudded said

    Very nice!!

  2. […] Questo articolo mostra come ovviare a questa mancanza… […]

  3. […] Display site members from AD Groups « Bits of Sharepoint […]

  4. Kieran said

    Hi Jessica

    When you did this did you inherit from an existing web part i.e. the Site Users web part, or did you roll your own one for this?

    All the best

    • Jessica Wang said

      This is custom web part as the OOTB web part that SharePoint provides will not show users that are in an AD group.

      public class HelloWebPart : WebPart
      private string text= “Hello World!”;

      [WebBrowsable(true), Personalizable(true)]
      public string text
      get { return text; }
      set { text= value; }

      protected override void Render(System.Web.UI.HtmlTextWriter w)


  5. Kieran said

    Thanks Jessica.

    Do you think this approach could be used by inheriting from the existing site users web part and then overriding the method that retrieves the groups from SharePoint. I’m just guessing but it would depend on being able to inherit from the existing OOTB web part.

    The main reason for this is that I want to display a picture with each persons details something that the OOTB web part already does.

    All the best.

  6. usman said

    great post Heather
    A strange thing is happening; code runs smoothly in a console application; but when put the same code in a assembly and deploy it into GAC, i get following error on this line
    SearchResultCollection coll = srch.FindAll();

    An operations error occurred.\r\n”} System.Exception {System.Runtime.InteropServices.COMException}

    StackTrace ” at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)\r\n at System.DirectoryServices.DirectoryEntry.Bind()\r\n at System.DirectoryServices.DirectoryEntry.get_AdsObject()\r\n at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)\r\n at NavigationSourceData.SPMenu.isUserInGroup(String strGroup, String username) in d:\filename.cs:line 719″ string

    Any ideas will be really appreciated

  7. I had fun reading this post. I want to see more on this subject.. Thanks for writing this nice article.. In Any Event, Im going to subscribe to your rss and I wish you write great articles again soon.

  8. […] Write a custom Web Part a la this blog post […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: