2

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

Yazid Erman
  • 1,166
  • 1
  • 13
  • 24

3 Answers3

3

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

SureshGowtham S
  • 686
  • 5
  • 10
2

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;
Victor Ivanidze
  • 397
  • 2
  • 7
  • 1
    Does this mean that it will parse the message body and extract any mentioned contacts? – Yazid Erman Dec 24 '18 at 11:31
  • Anyway, I am interested in the the list of the contacts the account has, not related to the current Item. – Yazid Erman Dec 24 '18 at 15:23
  • I wonder if using the Open Graph API is the solution? although it doesn't look like we have to register an Office Add-In: https://learn.microsoft.com/en-us/graph/api/user-list-contacts?view=graph-rest-1.0 – Yazid Erman Dec 24 '18 at 15:36
  • Yazid, an account has no contacts at all. It has one or more addresses. What do you mean exactly? – Victor Ivanidze Dec 24 '18 at 16:50
  • I mean the outlook contacts list that is managed by each user: the subject of this link: https://www.add-in-express.com/creating-addins-blog/2011/10/27/get-outlook-contacts/ I have updated the main question to make it clear – Yazid Erman Dec 25 '18 at 05:54
  • You can use REST API, as Alina has suggested, or EWS. – Victor Ivanidze Dec 25 '18 at 08:42
0

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

Alina Li
  • 884
  • 1
  • 6
  • 5
  • Thank you Alina, but I am working on Office Add-In and Javascript is my language, not C#, do you know the SDK for Javascript? – Yazid Erman Dec 25 '18 at 06:11
  • You could refer to this link: https://stackoverflow.com/questions/39029053/getting-a-count-of-contacts-from-outlook-office-rest-api-javascript . – Alina Li Dec 25 '18 at 06:14