Active Directory

P

Peter

I am trying to get values from Global Address List in Outlook through Active Directory

I am able to get all the values except the 'location'. There seems to be no 'location' variable

Is there a way to retreive the location form GAL?

Here is the code:

DirectoryEntry entry = new DirectoryEntry(_adPath, _adUserName, _adPassword, AuthenticationTypes.Secure);
//
// search for samAccountName
//
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("("+criteria+")");
//mySearcher.Filter = ("(ObjectClass=user)");
mySearcher.PropertiesToLoad.Add("department");
mySearcher.PropertiesToLoad.Add("telephoneNumber");
mySearcher.PropertiesToLoad.Add("location"); // <--
mySearcher.PropertiesToLoad.Add("mail");

//
//done list
//
SearchResultCollection resEnt = mySearcher.FindAll();
string location = result.Properties["location"][0].ToString(); // <-- always blank
Thank You



Peter
 
Y

Yuan Ren[MSFT]

Hi Peter,

Welcome to MSDN newsgroup!

Based on my understanding, your issue is the value of location property
always display blank when retrieving from GAL. Please let me know if I have
misunderstood something.
I have performed the test but can not repro the problem. As far as I know,
although we already specify the properties in our search, the "location" is
not required. So I suggest the better way is do some checking before we get
its value, like below code:
if (result.Properties.Contains("location"))
string location = result.Properties["location"].Value.ToString();
else
//do something here

Additional, to prevent the problem caused by interop, we could make a
script application first to do the same test.

I hope the above information helps, if you have any questions or concerns,
please do not hesitate to let me know. I am looking forward your reply.

Regards,

Yuan Ren
Microsoft Online Support
 
Y

Yuan Ren[MSFT]

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
 
P

Peter

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
 
M

Marc Scheuner [MVP ADSI]

I am trying to get values from Global Address List in Outlook through Active Directory
I am able to get all the values except the 'location'. There seems to be no 'location' variable

You might want to have a look at the excellent Excel sheets that my
fellow MVP Richard Mueller has put together, outlining each and every
AD property, and where it maps to in terms of the ADU&C GUI:

http://www.rlmueller.net/UserAttributes.htm

If that doesn't help - you could also download my Beavertail ADSI
Browser and inspect your Outlook GAL in AD, and figure out where that
location is stored.

http://adsi.mvps.org/adsi/CSharp/beavertail.html

It's free, 100% C#, and yours to enjoy

Marc
 
P

Peter

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
 
Y

Yuan Ren[MSFT]

Thanks for Marc's suggestion. The tool seems be cool!

If you have any other concerns, please feel free to let me know.. It's my
pleasure to be of assistance.

Yuan Ren [MSFT]
Microsoft Online Support
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top