Yazid Erman
Yazid Erman

Reputation: 1186

Is it possible to programmatic-ally access the list of contacts in outlook using Office Add In

I am building an Add In which is supposed to grab in addition to the list of contacts an account has, the contacts (to, from, cc and bcc) that are used in the current Item (Message).

As per the documentation, the following instruction gave me zero contacts, although I have contacts in the contacts book, and reading a message with a sender email.

var contacts = Office.context.mailbox.item.getEntities().contacts;

I need to grab the list of contacts I manage in my account: enter image description here

This list is accessible with the open graph APIs, I wonder if it's also accessible locally with the Office object for Office Add-Ins

Upvotes: 2

Views: 1160

Answers (3)

SureshGowtham S
SureshGowtham S

Reputation: 686

Office Js does not provide APIs to get the list of contacts in the account. But you can get an auth token from Outlook using authentication APIs, then use this token to acquire Graph token to interact with Graph APIs and get the list of contacts

Office.context.auth.getAccessTokenAsync(function (result) {
    if (result.status === "succeeded") {
        // Use this token to call Web API
        var ssoToken = result.value;
        // Now send this token to your server and acquire a Graph token
        // Server can talk to Graph APIs and get contacts to display
    } else {
        // Handle error
    }
});

Create a Node.js Office Add-in that uses single sign-on

Upvotes: 3

Alina Li
Alina Li

Reputation: 884

You could get all contacts using the below link:

Microsoft.Office.Interop.Outlook.Items OutlookItems;
  Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
  MAPIFolder Folder_Contacts;
  Folder_Contacts = (MAPIFolder)outlookObj.Session.GetDefaultFolder(OlDefaultFolders.olFolderContacts);
  OutlookItems = Folder_Contacts.Items;
  MessageBox.Show("Wykryto kontaktów: " + OutlookItems.Count.ToString());

  for (int i = 0; i < OutlookItems.Count; i++)
  {
    Microsoft.Office.Interop.Outlook.ContactItem contact = (Microsoft.Office.Interop.Outlook.ContactItem)OutlookItems[i+1];
    sNazwa = contact.FullName;
    sFirma = contact.CompanyName;
    sAdress = contact.BusinessAddressStreet;
    sMiejscowosc = contact.BusinessAddressPostalCode + " " + contact.BusinessAddressCity;
    sEmail = contact.Email1Address;
    dataGridView1.Rows.Add(sNazwa, sFirma, sAdress, sMiejscowosc, sEmail);

  }

For more information, please refer to the below link:

Get Outlook contacts into C# form-based application

Upvotes: 0

Victor Ivanidze
Victor Ivanidze

Reputation: 411

It looks you misunderstood the documentation.

A quote:

The following example accesses the contacts entities in the current item's body.

var contacts = Office.context.mailbox.item.getEntities().contacts;

Upvotes: 2

Related Questions