Posted by: AlphaAlien on Fri, Mar 28th, 2008
I wrote a bit of code this morning to update an old web service I wrote that returns users from active directory along with some key data. If you’re interested in a web service that returns a users account name, phone, email and name read on for the sample! The code is fairly straightforward and self explanatory if you need help just respond back.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.DirectoryServices;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace Utilities
{
///
/// GetUserInfoFromAD is a web service that pulls user information from Active Directory based on the user's NT ID.
/// The information retrieved includes the email, display name, and cost center of the user.
///
[WebService(Namespace = "http://it-sp-dev/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class GetUserInfoFromAD : System.Web.Services.WebService
{
[WebMethod]
public DataTable ldapsearch(string query)
{
// Search adPath using this string
string sFilter = String.Format("(&(objectClass=user)(mail={0}))", "*"+ query + "*");
//these are the attributes that will show
string[] attribs = new string[] { "SAMAccountName", "Name", "TelephoneNumber", "Mail" };
// Search using these filters
DirectorySearcher ds = new DirectorySearcher("");
ds.Filter = sFilter;
DataTable dt = new DataTable("ADLookup");
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Phone", typeof(int));
dt.Columns.Add("Email", typeof(string));
foreach (SearchResult search in ds.FindOne())
{
DirectoryEntry de = search.GetDirectoryEntry();
DataRow dr = dt.NewRow();
dr["UserName"] = de.Properties["sAMAccountName"].Value.ToString();
dr["Name"] = de.Properties["Name"].Value.ToString();
dr["Phone"] = de.Properties["TelephoneNumber"].Value.ToString();
dr["Email"] = de.Properties["Mail"].Value.ToString();
dt.Rows.Add(dr);
}
return dt;
}
}
}
Comments: (2)
on Wed, May 07th, 2008 at 09:51 AM
Very cool!
on Tue, Jun 17th, 2008 at 06:05 PM
looks like a great tutorial but i’m just too dumb to follow the instructions
Page 1 of 1 pages


















Post Tags: microsoft active directory code
Next entry: Stupid SharePoint Tricks with Cross-Site DataViews Previous entry: Fixing RunAs in Vista