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

    • 59,985 hits
  • Advertisements

Posts Tagged ‘Active Directory’

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;}


Posted in WebParts | Tagged: , , , , | 10 Comments »