Populate a dropdownlist with members of a security group in ASP.NET(VB)

Posted on 2009-02-10
Last Modified: 2013-11-26
I've got a security group called MANAGERS. How would I populate a dropdownlist with the names from this group?
Question by:mwmiller78
    LVL 39

    Expert Comment

    by:Kyle Abrahams
    Is it an AD group?  If not what group?
    LVL 15

    Expert Comment

    by:Praveen Venu

     public static DataSet GetUsersForGroup(string GroupName)
    DataSet dsUser = new DataSet();
    DirectoryEntry de = GetDirectoryObject();
    //create instance fo the direcory searcher
    DirectorySearcher deSearch = new DirectorySearcher();
    //set the search filter
    deSearch.SearchRoot =de;
    deSearch.Filter = "(&(objectClass=group)(cn=" + GroupName +"))";
    //get the group result
    SearchResult results= deSearch.FindOne();
    //Create a new table object within the dataset
    DataTable tbUser = dsUser.Tables.Add("Users");
    //Create default row
    DataRow rwDefaultUser = tbUser.NewRow();
    rwDefaultUser ["UserName"]= "0";
    rwDefaultUser ["DisplayName"]="(Not Specified)";
    rwDefaultUser ["EMailAddress"]="(Not Specified)";
    //if the group is valid, then continue, otherwise return a blank dataset
    if(results !=null)
    //create a link to the group object, so we can get the list of members
    //within the group
    DirectoryEntry deGroup= new DirectoryEntry(results.Path,ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
    //assign a property collection
    System.DirectoryServices.PropertyCollection pcoll = deGroup.Properties;
    int n = pcoll["member"].Count;
    //if there are members fo the group, then get the details and assign to the table
    for (int l = 0; l < n ; l++)
    //create a link to the user object sot hat the FirstName, LastName and SUername can be gotten
    DirectoryEntry deUser= new DirectoryEntry(ADFullPath + "/" +pcoll["member"][l].ToString(),ADAdminUser,ADAdminPassword,AuthenticationTypes.Secure);
    //set a new empty row
    DataRow rwUser = tbUser.NewRow();
    //populate the column
    rwUser["UserName"]= GetProperty(deUser,"cn");
    rwUser["DisplayName"]= GetProperty(deUser,"givenName") + " " + GetProperty(deUser,"sn");
    rwUser["EMailAddress"]= GetProperty(deUser,"mail");
    //append the row to the table of the dataset
    //close the directory entry object
    return dsUser;

    Open in new window


    Author Comment

    Yes, an AD group.
    LVL 39

    Accepted Solution

    Below is the attached VB.Net code.

    instead of slGroupMembers.Add you could just add it write to your drop down list.

    Public Shared Function GetADS_Group_Members(ByVal strGroup_Name As
    String) As SortedList
    Dim deGroup As DirectoryEntry
    Dim MembersCollection As IADsMembers
    Dim member As IADsUser
    Dim objRootDSE As New DirectoryEntry("LDAP://RootDSE")
    Dim slGroupMembers As New SortedList
    deGroup = New DirectoryServices.DirectoryEntry("LDAP://CN=" &
    strGroup_Name & ",OU=User Groups,OU=Groups,OU=SOMEOU1,OU=SOMEOU2," &
    objRootDSE.Properties("defaultNamingContext")(0).T oString())
    MembersCollection = CType(deGroup.Invoke("Members"),
    For Each member In MembersCollection
    slGroupMembers.Add(member.LastName, Replace(member.Name,
    "CN=", "", , , CompareMethod.Text) & "|;|" & member.ADsPath)
    Catch ex As Exception
    If Not IsNothing(deGroup) Then deGroup.Close()
    deGroup = Nothing
    End Try
    Return slGroupMembers
    End Function

    Open in new window


    Author Comment

    What's the deal with IADsMembers and IADsUser? What ref/namespace would I use for those?
    LVL 39

    Expert Comment

    by:Kyle Abrahams

    See post November 19th, 2005, 05:32 PM

    Imports System
    Imports System.DirectoryServices
    Imports System.Web.HttpContext
    Imports System.Collections
    Imports ActiveDs


    Author Comment

    I keep getting Index out of range on the following

    deGroup = New DirectoryServices.DirectoryEntry("LDAP://CN=" &
    strGroup_Name & ",OU=User Groups,OU=Groups,OU=SOMEOU1,OU=SOMEOU2," &
    objRootDSE.Properties("defaultNamingContext")(0).T oString())

    Have you actually used this?

    Author Comment

    Nevermind, I got it. I just took off

    ," &objRootDSE.Properties("defaultNamingContext")(0).ToString()

    and it worked fine. Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now