Sunday, July 5, 2009

Look For Users in Active directory,in Sharepoint

This code looks for a User in Active directory and Pull up the users Name and E-mail address, if it exists.

//Getting The User Details From Active Directory

Private GetUser()
{
SPUser usr = null;

if (txtUSerName.Text != null)
{
SPSite mySite = SPContext.Current.Site;
SPWeb myWeb = mySite.OpenWeb();

//Using RunWithElevatedPrivileges to get the owner below.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
// Get references to the site collection and site for the current context.
// The using statement makes sures these references are disposed properly.

using (SPSite siteCollection = new SPSite(mySite.ID))
{

using (SPWeb web = siteCollection.OpenWeb(myWeb.ID))
{

web.AllowUnsafeUpdates = true;

try
{
usr = web.EnsureUser(txtUserName.Text);

//usr.Name to pull its name
//usr.emailid tp pull its e-mail
}
catch (Exception exp)
{
usr = null;
}
web.AllowUnsafeUpdates = false;
//siteCollection = null;
//web = null;
}
}
});

}

Note: The code is to check the users in AD not in sharepoint.

No comments:

Post a Comment