Thank you very much
You got my answer!!!
Here's my code:
using System;
using System.Web;
using System.Web.Mail;
using System.Security.Principal;
using System.DirectoryServices;
using Error_Logger;
using System.Collections;
namespace Test
{
public void Department(ADInfo adInfo)
{
//string department = null;
string criteria = "samaccountname=" + adInfo.SamAccount;
//
// log into AD
//
//_adPath = "LDAP://test.com,OU=TestAdmin,OU=US,OU=NorthAmerica,OU=Accounts,DC=test,DC=com";
try
{
DirectoryEntry entry = new DirectoryEntry(_adPath, _adUserName, _adPassword, AuthenticationTypes.Secure);
//
// search for samAccountName
//
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("("+criteria+")");
mySearcher.PropertiesToLoad.Add("department");
mySearcher.PropertiesToLoad.Add("telephoneNumber");
mySearcher.PropertiesToLoad.Add("location");
mySearcher.PropertiesToLoad.Add("mail");
adInfo.EmailResolved = "0";
//
//done list
//
SearchResultCollection resEnt = mySearcher.FindAll();
if(resEnt.Count > 0)
{
adInfo.EmailResolved = "1";
foreach(SearchResult result in resEnt)
{
try
{
adInfo.Department = result.Properties["department"][0].ToString();
}
catch
{ ; }
try
{
adInfo.Email = result.Properties["mail"][0].ToString();
}
catch
{ ; }
try
{
adInfo.Phone = result.Properties["telephoneNumber"][0].ToString();
}
catch
{ ; }
try
{
adInfo.Location = result.Properties["location"][0].ToString();
}
catch
{ ; }
//break;
}
}
}
catch(Exception e)
{
ErrorLogger.LogErrorToFile("Department - " + adInfo.SamAccount + " - " + e.ToString());
}
}
using System;
namespace Test
{
/// <summary>
/// ADInfo class holds Active Directory info
/// </summary>
public class ADInfo
{
string _samAccount = string.Empty; // Windows SAM account
string _department = string.Empty; // department from GAL
string _email = string.Empty; // email address from GAL
string _phone = string.Empty; // phone # from GAL
string _emailResolved = string.Empty; // flag indicating email was found
string _location = string.Empty; // work location
public ADInfo()
{
}
/// <summary>
/// returns _department + _email + _phone for string comparison
/// </summary>
public string ADString
{
get { return _department + _email + _phone + _location + (_emailResolved.Equals("0") ? "False" : "True") ; }
}
/// <summary>
/// set / get department
/// </summary>
public string Department
{
set { _department = value; }
get { return _department; }
}
/// <summary>
/// set / get Sam Account
/// </summary>
public string SamAccount
{
set { _samAccount = value; }
get { return _samAccount; }
}
/// <summary>
/// set / get Email address
/// </summary>
public string Email
{
set { _email = value; }
get { return _email; }
}
/// <summary>
/// set / get Phone #
/// </summary>
public string Phone
{
set { _phone = value; }
get { return _phone; }
}
public string EmailResolved
{
set { _emailResolved = value; }
get { return _emailResolved; }
}
/// <summary>
/// set / get Location
/// </summary>
public string Location
{
set { _location = value; }
get { return _location; }
}
}
}
"Yuan Ren[MSFT]" said:
Hi Peter,
Thanks for your posting!
For the current issue, it seems the object of property "location" is null,
so when you want to get the value, the error occurred.
In my opinion, the snippet of code is not enough for trouble shooting.
Could you please provide the whole operation code to make me clear about
the current issue? It can help me to make a demo how to get the location
value.
I'm looking forward your reply, if you have any concern, please let me know.
Regards,
Yuan Ren [MSFT]
Microsoft Online Support