You must Sign In to post a response.
  • Category: Sharepoint

    How to get users from AD Group ?

    Hi,

    how to get users from ad group in vs2010 for sharepoint foundation wsp ?

    Thanks,

    Aasai
  • #730213
    Hi,

    Try the following code which is working in Asp.net

    //Page load of your Aspx page
    ArrayList groupMembers = new ArrayList();
    groupMembers = GetAllUsers("YourGroupName");
    for (int i = 0; i < groupMembers.Count; i++)
    {
    Response.Write(i.ToString() + ". " + groupMembers[i].ToString() + "<br/>");
    }

    //Create a generic method to fetch users for the given group
    static public ArrayList GetAllUsers(string strGroupName)
    {
    ArrayList groupMembers = new ArrayList();
    searchedGroups = new Hashtable();

    // First you need to find the group distinguished name from LDAP
    DirectorySearcher search = new DirectorySearcher("LDAP://DC=YourCompanyDomain,DC=com");
    search.Filter = String.Format("(&(objectCategory=group)(cn={0}))", strGroupName);
    search.PropertiesToLoad.Add("distinguishedName");
    SearchResult sru = null;
    DirectoryEntry group;
    try
    {
    sru = search.FindOne();
    }
    catch (Exception ex)
    {
    throw ex;
    }
    group = sru.GetDirectoryEntry();

    //Once you found the distinguised name for the given group, now you need to pass that distinguished group and you can //get the users in that group
    groupMembers = getUsersInGroup(group.Properties["distinguishedName"].Value.ToString());

    return groupMembers;
    }

    private static ArrayList getUsersInGroup(string strGroupDN)
    {
    ArrayList groupMembers = new ArrayList();

    // find all users in this group
    DirectorySearcher ds = new DirectorySearcher("LDAP://DC=YourCompanyDomain,DC=com");
    ds.Filter = String.Format("(&(memberOf={0})(objectClass=person))", strGroupDN);
    ds.PropertiesToLoad.Add("samaccountname");

    foreach (SearchResult sr in ds.FindAll())
    { groupMembers.Add(sr.Properties["samaccountname"][0].ToString());
    }
    return groupMembers;
    }

  • #730228
    Hi.
    You may use contact class to get the user details, but I am not sure whether it will support for the sharepoint foundation wsp or not. As per my understanding it is not.


    Regards,
    Asheej T K

  • #730274
    Thanks Mahesh & Asheej.

    I have get multiple users & group from people picker. so First i want to find the type. after that only i can use directory class.

    You are right asheej. I have used contact class for getting users from group & find input type(user type as a sp group, ad group or user).

    But it is not support on foundation server.

    Please Advice.

  • #730280
    You need to use 'System.DirectoryServices' namespace and A 'DirectorySearcher' class will help you to fetch the list of users from AD.
    may be the following links help you better to accomplish your task
    http://sharepoint.stackexchange.com/questions/31485/pull-all-active-directory-users-into-sharepoint-2010
    http://stackoverflow.com/questions/7886772/get-all-users-from-active-directory-to-sharepoint
    http://www.codeproject.com/Tips/599697/Get-list-of-Active-Directory-users-in-Csharp

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #730287
    hey,
    you can use this refrence for this,

    http://www.codeproject.com/Articles/6778/How-to-get-User-Data-from-the-Active-Directory

    http://shermanstechnicalblog.blogspot.in/2012/09/programmatically-obtain-user.html

    http://social.msdn.microsoft.com/Forums/sharepoint/en-us/home?category=sharepoint

    http://stackoverflow.com/questions/4314767/getting-members-of-an-ad-domain-group-using-sharepoint-api

    Thanks,
    ketan

  • #730318
    private void GetUserProfile1(SPUser user)
    {


    try
    {
    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
    {
    using (SPWeb web = site.OpenWeb())
    {

    web.AllowUnsafeUpdates = true;

    SPServiceContext oSPServiceContext = SPServiceContext.GetContext(site);
    UserProfileManager upm = new UserProfileManager(oSPServiceContext);
    SPUser spUser = null;
    spUser = web.EnsureUser(user.ToString());
    UserProfile profile = upm.GetUserProfile(spUser.LoginName);//web.CurrentUser.LoginName);
    {



    if (profile[PropertyConstants.AccountName].Value != null)
    impEmpno = profile[PropertyConstants.AccountName].ToString();




    //if (profile[PropertyConstants.HireDate].Value != null)
    // lblDateValue.Text = profile[PropertyConstants.HireDate].ToString();

    }

    }
    }
    }
    catch (Exception ex)
    {

    string Message = string.Empty;
    Message = "II Implementor Dont Have User Profile";
    Page.ClientScript.RegisterClientScriptBlock(typeof(SPAlert), "alert", "<script language=\"javascript\">alert('" + Message + "')</script>");

    }
    }


  • Sign In to post your comments