views:

29

answers:

2

Hi All !!!!!!!

I want to get user names in active directory by using sharepoint webpart. any way a i can get usernames by using ASP. net but i cant convert that code in to sharepoint web part....

here is the code .........

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Web;
          using System.Web.UI;
          using System.Web.UI.WebControls;
          using System.DirectoryServices;
          using System.Data;


        string domain = "LDAP://serverName";
        DirectoryEntry entry = new DirectoryEntry(domain);

        DirectorySearcher searcher = new DirectorySearcher(entry);

        searcher.Filter = "(&(objectClass=user))";

        SearchResultCollection resultCol = searcher.FindAll();

        //Link list for store user names 
        List<String> User_Names = new List<String>();

        int count = 0;

        foreach (SearchResult result in resultCol)
        {
            User_Names.Add(result.Properties["CN"][0].ToString());
            count = count + 1;
        }

        //can print all user names by using for loop or while loop 

        Label2.Text = RadioButtonList1.SelectedItem.Text;
A: 

Here's a little something to get you started. It's a skeleton for a .NET web part class that includes a data-bound RadioButtonList. To get it deployed to SharePoint, you will also need to roll together a proper .webpart manifest. There are lots of examples of these on the web, and Visual Studio 2010 projects for SharePoint 2010 make it super easy with the Visual Web Part template. (Note that if you are using the Visual Web Part template, then you can write up the web part with an .ascx file like you would any other user control, instead of using CreateChildControls() as I do below.)

Good luck!

using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// This web part provides redirection logic to a page.
/// </summary>
public class RedirectWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
    /// <summary>
    /// Creates the child controls.
    /// </summary>
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
        RadioButtonList list = new RadioButtonList();
        Controls.Add(list);

        list.AutoPostBack = true;
        list.SelectedIndexChanged += new EventHandler(this.OnSelectedNameChanged);

        // On the next line, ADUtility.GetActiveDirectoryNames() is a dummy
        // class and method that you should replace with your AD query logic.
        // It just needs to return an enumerable collection of strings.
        list.DataSource = ADUtility.GetActiveDirectoryNames();
        list.DataBind();
    }

    protected void OnSelectedNameChanged(object sender, EventArgs e)
    {
        // Your event-handling logic.
    }
}
kbrimington
A: 

Hi All .....,

        //don't forget to add System.DirectoryServices.AccountManagement as reference and using System.DirectoryServices.AccountManagement;

        PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MyDomain","DC=MyDomain,DC=com");
        UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext);
        insUserPrincipal.Name = "*";
        PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher();
        insPrincipalSearcher.QueryFilter = insUserPrincipal;
        PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll();
        foreach (Principal p in results)
        {
            Console.WriteLine(p.Name);
        }

Above Code worked for me.... but i had to do some modifications (1) Remove the ,"DC=MyDomain,DC=com" part and save it and Deploy the web part

(2) In the sharepoint web part there is no any Console.Writeline ("String") so change it whatever you want....

Note - This code is showed all the users in the Avtive Directory if the user is logged or not....

Cheers !!!!!

Chinthaka

chinthaka