简体   繁体   中英

Retrieve Contacts using Microsoft Exchange WebServices from Outlook

I am using Microsoft Exchange Services to retrieve contacts from Outlook. I use the following code. It executes without any error. However, I get nothing in Contacts.

public ActionResult Index()
    {
        ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        _service.Credentials = new WebCredentials("username", "password");
        _service.AutodiscoverUrl("****");
        _service.Url = new Uri("https://***/EWS/Exchange.asmx");
        foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue)))
        {
            // do something 
        }
        return View();
    }

How can i get the contacts?

Please help, Thanks.

I would suggest you clean up your code as there a few reason I can see it failing eg

    _service.AutodiscoverUrl("****");
    _service.Url = new Uri("https://***/EWS/Exchange.asmx");

use one or the other and for AutoDiscoverURL you may need a callback in here

    foreach (Contact contact in _service.FindItems(WellKnownFolderName.Contacts, new ItemView(int.MaxValue)))

First of all a Contacts folder can contain objects other then Contacts so if your code comes across a Distribution list its going to generate an exception. Also using int.MaxValue is a bad idea you should page the Items in groups of 1000 (Throttling on Exchange 2010 will enforce this for you anyway so your code will just fail to get all the Contacts if there are more then 1000). Also does the Mailbox your trying to access belong to the security credentials your using. I would suggest you use something like

        String mailboxToAccess = "user@domain.onmicrosoft.com";
        ExchangeService _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
        _service.Credentials = new WebCredentials("upn@domain.onmicrosoft.com", "password");
        _service.AutodiscoverUrl(mailboxToAccess, redirect => true);
       // _service.Url = new Uri("https://***/EWS/Exchange.asmx");
        ItemView iv = new ItemView(1000);
        FolderId ContactsFolderId = new FolderId(WellKnownFolderName.Contacts,mailboxToAccess);
        FindItemsResults<Item> fiResults;
        do
        {
            fiResults = _service.FindItems(ContactsFolderId, iv);
            foreach (Item itItem in fiResults.Items)
            {
                if (itItem is Contact)
                {
                    Contact ContactItem = (Contact)itItem;
                    Console.WriteLine(ContactItem.Subject);
                }
            }
            iv.Offset += fiResults.Items.Count;
        } while (fiResults.MoreAvailable);

You can test EWS itself using the EWSEditor http://ewseditor.codeplex.com/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM