Muokkaa

Jaa


Get an Outlook item's attachments from Exchange

The Office JavaScript API includes APIs to get attachments and their contents from messages and appointments in Outlook. The following table lists these attachment APIs, the Outlook modes they operate in, and the minimum Mailbox requirement set they need to operate.

API Supported Outlook modes Minimum requirement set
Office.context.mailbox.item.attachments Read 1.1
Office.context.mailbox.item.getAttachmentsAsync Compose 1.8
Office.context.mailbox.item.getAttachmentContentAsync Read
Compose
1.8

If the Outlook client in which the add-in is running doesn't support the needed minimum requirement set, you can get an attachment and its contents directly from Exchange instead. Select the tab for the applicable Exchange environment.

In Exchange Online environments, your add-in must perform the following steps to get attachments directly from Exchange.

  1. Get an access token to Microsoft Graph.
  2. Get the item ID of the applicable message or appointment.
  3. Use Microsoft Graph to get the attachment and its properties.

Each step is covered in the following sections.

Get an access token

Microsoft Graph provides access to users' Outlook mail data. Before your add-in can obtain data from Microsoft Graph, it must first get an access token for authorization. To get an access token, use nested app authentication (NAA). To learn more about NAA, see Enable SSO in an Office Add-in using nested app authentication (preview).

Get the item ID of the mail item

To get information about an attachment using Microsoft Graph, you need the item ID of the message or appointment that includes the attachment. Use the applicable Office JavaScript API to get the item ID.

  • Read mode: Call Office.context.mailbox.item.itemId. On non-mobile Outlook clients, because this property returns an ID formatted for Exchange Web Services (EWS), you must use the Office.context.mailbox.convertToRestId method to convert the ID into a REST format that Microsoft Graph can use.

    // Get the item ID of the current mail item in read mode and convert it into a REST format.
    const itemId = Office.context.mailbox.item.itemId;
    const restId = Office.context.mailbox.convertToRestId(itemId, Office.MailboxEnums.RestVersion.v2_0);
    
  • Compose mode: The method to get the item ID varies depending on whether the mail item has been saved as a draft.

    • If the item has been saved, call Office.context.mailbox.item.getItemIdAsync.

      // Get the item ID of the current mail item being composed.
      Office.context.mailbox.item.getItemIdAsync((result) => {
          if (result.status === Office.AsyncResultStatus.Failed) {
              console.error(result.error.message);
              return;
          }
      
          const itemId = result.value;
      });
      

      Tip

      The getItemIdAsync method was introduced in Mailbox requirement set 1.8. If the Outlook client in which your add-in is running doesn't support Mailbox 1.8, use Office.context.mailbox.item.saveAsync instead as this method was introduced in Mailbox 1.3.

    • If the item hasn't been saved yet, call Office.context.mailbox.item.saveAsync to initiate the save and get the item ID.

      // Save the current mail item being composed to get its ID.
      Office.context.mailbox.item.saveAsync((result) => {
          if (result.status === Office.AsyncResultStatus.Failed) {
              console.error(result.error.message);
              return;
          }
      
          const itemId = result.value;
      });
      

      Note

      If your Outlook client is in cached mode, it may take some time for the saved item to sync to the server. Until the item is synced, using the item ID will return an error.

Use Microsoft Graph

Once you've obtained an access token and the item ID of the mail item containing the attachment, you can now make a Microsoft Graph request. For information and examples on how to get an attachment using Microsoft Graph, see Get attachment.

See also