'Getting contacts from Exchange instead of Outlook

I am currently developing an application to be used internally only at work. I need to get the currently logged in user's contacts to use in the application and I am currently getting the contacts with the following:

Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application();
            Microsoft.Office.Interop.Outlook.NameSpace NameSpace = app.GetNamespace("MAPI");
            Microsoft.Office.Interop.Outlook.MAPIFolder ContactsFolder = NameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts);
            Microsoft.Office.Interop.Outlook.Items ContactItems = ContactsFolder.Items;
            foreach (Microsoft.Office.Interop.Outlook.ContactItem item in ContactItems)
            {

                //do stuff with the contacts here

            }

The problem with this approach is that whenever a user opens the application and Outlook is not already open, an Outlook popup appears asking the user to Allow or Deny the application's access to Outlook contacts. This is unnecessary and my only thought of how to stop this form happening is instead of using Outlook itself, get the contacts from the Exchange Server.

I have looked into a bunch of documentation for things like EWS however I have not found reference for EWS to be guaranteed working with Exchange 2019. I would also like any authentication done automatically based on domain authentication with the currently logged in user instead of requiring the user to input a password.

I did try to use this: https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications however ExchangeVersion only has options up to Exchange 2013.

What should I be using to achieve this? Any push in the right direction would be greatly appreciated.



Solution 1:[1]

Use the active directory instead of EWS to get network users data, including email address.
the relevant namespace is: System.DirectoryServices
Here is an example that I wrote in my project to get user data including email by the first name and last name from AD.
Note: ActiveDirectoryEntity us a class of mine.
also, regarding another issue you wrote in your question, entering the user and password is not needed because authentication was already maid when the user authenticated to windows.

public static List<ActiveDirectoryEntity> GetActiveDirectoryData(string sname, string fname)
{
    try
    {
        DirectorySearcher search = new DirectorySearcher();
        search.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(givenname={0}*)(sn={1}*))", sname, fname);
        search.PropertiesToLoad.Add("givenName");
        search.PropertiesToLoad.Add("sn");
        search.PropertiesToLoad.Add("mail");
        search.PropertiesToLoad.Add("mobile");
        search.PropertiesToLoad.Add("department");

        var result = search.FindAll();

        List<ActiveDirectoryEntity> resultlist = new List<ActiveDirectoryEntity>();

        foreach (SearchResult r in result)
        {
            if (r.Properties["mail"] != null)
            {
                if (r.Properties["mail"].Count > 0)
                {
                    ActiveDirectoryEntity ade = new ActiveDirectoryEntity();
                    if ((r.Properties["givenname"].Count > 0))
                        ade.FirstName = r.Properties["givenName"][0].ToString();
                    if ((r.Properties["sn"].Count > 0))
                        ade.LastName = r.Properties["sn"][0].ToString();
                    if ((r.Properties["mail"].Count > 0))
                        ade.Email = r.Properties["mail"][0].ToString();
                    if ((r.Properties["department"].Count > 0))
                        ade.Department = r.Properties["department"][0].ToString();
                    resultlist.Add(ade);
                }
            }
        }

        return resultlist;
    }
    catch
    {
        return null;
    }

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1