locked
How to get the users from active directory group using C#? RRS feed

  • Question

  • Hi.,

    Could any one please help me on to get the users from active directory group?

    I am using .NET 3.5 framework and c# laungauge.

    Thanks for your advanced support.


    Thanks., Prakash

    Thursday, August 1, 2013 9:11 AM

Answers

  • You can use the System.DirectoryServices.AccountManagement namespace to access Active Directory data. 

    The MSDN documentation for it (.Net 3.5) is here: http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.groupprincipal(v=vs.90).aspx

    For Example: 

    var domainName = "yourdomain.com";
    var groupName = _query.Text;
    var queryUser = "fred";
    var queryUserPassword = "secretstuff";
    var principalContext = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName, queryUser, queryUserPassword);
    var members = groupPrincipal.GetMembers(true); //true to recursively get members


    Example Two (in a test webpart):

    public class QueryActiveDirectory : WebPart
    {
    	private Label _results;
    	private TextBox _query;
    	private Button _submit;
    
    	protected override void OnInit(EventArgs e)
    	{
    		base.OnInit(e);
    		_results = new Label();
    		_query = new TextBox();
    		_submit = new Button();
    		_submit.Text = "Submit";
    		_submit.Click += SubmitOnClick;
    	}
    
    	private void SubmitOnClick(object sender, EventArgs eventArgs)
    	{
    		try
    		{
    			var domainName = "yourdomain.com";
    			var groupName = _query.Text;
    			var queryUser = "fred";
    			var queryUserPassword = "secretstuff";
    			var principalContext = new PrincipalContext(ContextType.Domain, domainName);
    			GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName, queryUser, queryUserPassword);
    			if (groupPrincipal == null)
    			{
    				_results.Text = "The group was not found.";
    				return;
    			}
    			var members = groupPrincipal.GetMembers(true);
    			var membersList = new ArrayList();
    			foreach (Principal member in members)
    			{
    				membersList.Add(member.DisplayName);
    			}
    			var output = new StringBuilder();
    			output.AppendLine(String.Format("<span>Members of the {0} group:</span><br/>", groupName));
    			foreach (string s in membersList)
    			{
    				output.AppendLine(String.Format("<span>{0}</span><br/>", s));    
    			}
    			_results.Text = output.ToString();
    		}
    		catch (Exception ex)
    		{
    			_results.Text = String.Format("Error getting group from Active Directory. Error: {0}", ex.Message);
    		}
    	}
    
    	protected override void CreateChildControls()
    	{
    		Controls.Add(_query);
    		Controls.Add(_submit);
    		Controls.Add(new LiteralControl("<br/>"));
    		Controls.Add(_results);
    	}
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos

    Thursday, August 1, 2013 10:08 AM
  • Hi.,

    I got the solution. Here is the code,

    // Fetch the AD group members
    // Set up domain context
    PrincipalContext _Context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
    // Find the group in question
    GroupPrincipal _ADGroup = GroupPrincipal.FindByIdentity(_Context, IdentityType.Name, _SPGroupUserInfo.DisplayName);
    // If found....
    if (_ADGroup != null)
    {
    // Iterate over members
    foreach (Principal _ADUser in _ADGroup.GetMembers())
    {
    UserGrpTreeView.Nodes[index].ChildNodes[ChildIndex].ChildNodes.Add(new TreeNode(_ADUser.DisplayName));
    }
    }

    Try this if you want.


    Thanks., Prakash

    Thursday, August 1, 2013 10:09 AM

All replies

  • You can use the System.DirectoryServices.AccountManagement namespace to access Active Directory data. 

    The MSDN documentation for it (.Net 3.5) is here: http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.groupprincipal(v=vs.90).aspx

    For Example: 

    var domainName = "yourdomain.com";
    var groupName = _query.Text;
    var queryUser = "fred";
    var queryUserPassword = "secretstuff";
    var principalContext = new PrincipalContext(ContextType.Domain, domainName);
    GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName, queryUser, queryUserPassword);
    var members = groupPrincipal.GetMembers(true); //true to recursively get members


    Example Two (in a test webpart):

    public class QueryActiveDirectory : WebPart
    {
    	private Label _results;
    	private TextBox _query;
    	private Button _submit;
    
    	protected override void OnInit(EventArgs e)
    	{
    		base.OnInit(e);
    		_results = new Label();
    		_query = new TextBox();
    		_submit = new Button();
    		_submit.Text = "Submit";
    		_submit.Click += SubmitOnClick;
    	}
    
    	private void SubmitOnClick(object sender, EventArgs eventArgs)
    	{
    		try
    		{
    			var domainName = "yourdomain.com";
    			var groupName = _query.Text;
    			var queryUser = "fred";
    			var queryUserPassword = "secretstuff";
    			var principalContext = new PrincipalContext(ContextType.Domain, domainName);
    			GroupPrincipal groupPrincipal = GroupPrincipal.FindByIdentity(principalContext, groupName, queryUser, queryUserPassword);
    			if (groupPrincipal == null)
    			{
    				_results.Text = "The group was not found.";
    				return;
    			}
    			var members = groupPrincipal.GetMembers(true);
    			var membersList = new ArrayList();
    			foreach (Principal member in members)
    			{
    				membersList.Add(member.DisplayName);
    			}
    			var output = new StringBuilder();
    			output.AppendLine(String.Format("<span>Members of the {0} group:</span><br/>", groupName));
    			foreach (string s in membersList)
    			{
    				output.AppendLine(String.Format("<span>{0}</span><br/>", s));    
    			}
    			_results.Text = output.ToString();
    		}
    		catch (Exception ex)
    		{
    			_results.Text = String.Format("Error getting group from Active Directory. Error: {0}", ex.Message);
    		}
    	}
    
    	protected override void CreateChildControls()
    	{
    		Controls.Add(_query);
    		Controls.Add(_submit);
    		Controls.Add(new LiteralControl("<br/>"));
    		Controls.Add(_results);
    	}
    }


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos

    Thursday, August 1, 2013 10:08 AM
  • Hi.,

    I got the solution. Here is the code,

    // Fetch the AD group members
    // Set up domain context
    PrincipalContext _Context = new PrincipalContext(ContextType.Domain, Environment.UserDomainName);
    // Find the group in question
    GroupPrincipal _ADGroup = GroupPrincipal.FindByIdentity(_Context, IdentityType.Name, _SPGroupUserInfo.DisplayName);
    // If found....
    if (_ADGroup != null)
    {
    // Iterate over members
    foreach (Principal _ADUser in _ADGroup.GetMembers())
    {
    UserGrpTreeView.Nodes[index].ChildNodes[ChildIndex].ChildNodes.Add(new TreeNode(_ADUser.DisplayName));
    }
    }

    Try this if you want.


    Thanks., Prakash

    Thursday, August 1, 2013 10:09 AM