HOW TO: Get details about a Exchange User in Outlook 2007 & Outlook 2003

HOW TO: Get details about a Exchange User in Outlook 2007 & Outlook 2003

  • Comments 6

I recently came across an issue where I needed to get the details about Exchange User from within an Outlook Add-in. Below is a screen shot of the details I am referring to.

 image

In Outlook 2007 its very simple, just use the ExchangeUser Object and you will get the details. More details can be found in the article below:

ExchangeUser Object Members
http://msdn.microsoft.com/en-us/library/bb176658.aspx

Below is sample C# code that shows how to use the ExchangeUser Object:

using System;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace GetUserDetails
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlook.Application oApp;
            Outlook.NameSpace oNameSpace;
            Outlook.SelectNamesDialog oSNDialog;
            Outlook.AddressEntry oAEntry;
            Outlook.ExchangeUser oEUser;
    
            oApp = new Outlook.Application();
            oNameSpace = oApp.GetNamespace("MAPI");
            oNameSpace.Logon(null, null, true, false);  

            oSNDialog = oNameSpace.GetSelectNamesDialog();
            oSNDialog.NumberOfRecipientSelectors = Outlook.OlRecipientSelectors.olShowTo;
            oSNDialog.Caption = "Select User";
            oSNDialog.ToLabel = "User Name";
            oSNDialog.ShowOnlyInitialAddressList = true;
            oSNDialog.AllowMultipleSelection = false;
            oSNDialog.Display();
    
             foreach(Outlook.Recipient oRecipient in oSNDialog.Recipients)
             {
                oAEntry = oRecipient.AddressEntry;
                if(oAEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
                    || oAEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
                {
                    oEUser = oAEntry.GetExchangeUser();
                    Console.WriteLine(oEUser.Name);
                    Console.WriteLine(oEUser.BusinessTelephoneNumber);
                    Console.WriteLine(oEUser.MobileTelephoneNumber);
                    Console.WriteLine(oEUser.Department);

                    oEUser = null;
                }
                oAEntry = null;
             }

            oNameSpace.Logoff();
            oNameSpace = null;
            
        }
    }
}

However in Outlook 2003 the ExchangeUser Object is not there. The only way to do it is query the AD based on the AddressEntry.Address(LegacyexchangeDN). Below is sample C# code that uses the DirectoryServices namespace to query the AD based on LegacyexchangeDN.

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace GetUserDetailsBasedOnLegacyDN
{
    class Program
    {
        static void Main(string[] args)
        {
            //This is the legacy exchange DN
            string LEDN = "/O=ms/OU=east/cn=Recipients/cn=whatever";

            DirectoryEntry objRootDSE = new DirectoryEntry("GC://RootDSE");
            String strRoot = objRootDSE.Properties["rootDomainNamingContext"].Value.ToString();
            DirectoryEntry objRoot = new DirectoryEntry("GC://" + strRoot);
            
            objRoot.RefreshCache();

            DirectorySearcher objSrch = new DirectorySearcher(objRoot);
            objSrch.Filter = "(&(objectClass=user)(objectCategory=person)(legacyExchangeDN= " + LEDN + "))";
            objSrch.SearchScope = SearchScope.Subtree;
            DirectoryEntry objUser = new DirectoryEntry();
            objUser = objSrch.FindOne().GetDirectoryEntry();

            Console.WriteLine("Mobile:" + objUser.Properties["mobile"].Value);
            Console.WriteLine("Number:" + objUser.Properties["telephoneNumber"].Value);
            Console.WriteLine("Department:" + objUser.Properties["department"].Value);
            
            objUser = null;
            objSrch = null;
            objRoot = null;
            objRootDSE = null;
        }
    }
}

Note: I am not an AD expert, you might need to tweak the code depending on your environment and your needs.
Leave a Comment
  • Please add 4 and 7 and type the answer here:
  • Post
  • Thanks your example was useful to get the required details!!!

  • I need to fetch Home Number of the exchange user,

    Could you help me ?

  • Here you go!

    Outlook.PropertyAccessor oPA;

    String oHome1;

    oPA = oEUser.PropertyAccessor;

    //Get the Home Phone number

    oHome1 =(String)oPA.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3A09001E");

    Console.WriteLine(oHome1);

  • Hi,

    I am also facing the same problem, can you help me out how to use above code. what are the variable component in code, which I have to replace in code according to my AD.

  • The above code would work only for Outlook 2007. For Outlook 2003 you would have to use the "homePhone" property in the same way we query the "mobile" property.

  • First I'd Like to thank you for giving this code for OL2007, I was looking around for quite some time.

    But I have prb using the OL2007 code, when I double click on the Recipient name in the list, the dialog box disappears. The only way i can add it to the TO or CC box is to select the recipient and click on "UserName"

    Is there a way to double click on the name and it gets added to the To or CC box??

    Regards,

    Jesse

Page 1 of 1 (6 items)