-1

In a office web addin i have to remove multiple attachments. I try to use the Office.context.mailbox.item.removeAttachmentAsync document here: https://learn.microsoft.com/en-us/office/dev/add-ins/outlook/add-and-remove-attachments-to-an-item-in-a-compose-form

function removeAttachment(attachmentId) {

Office.context.mailbox.item.removeAttachmentAsync(
    attachmentId,
    { asyncContext: null },
    function (asyncResult) {
        if (asyncResult.status === Office.AsyncResultStatus.Failed) {
            write(asyncResult.error.message);
        } else {
            write('Removed attachment with the ID: ' + asyncResult.value);
        }
    });

}

How can i call this function in a loop and wait until all calls are finished? NOTE: async and await is no option because it also should work in IE11

Mario Semper
  • 511
  • 1
  • 3
  • 17

2 Answers2

0

I had similar problem with getAttachmentContentAsync... Try to use Promise.all - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all

I have a function witch returns promises like this (to read attachments content):

return new Promise<any>((resolve, reject) => {
  try {
    const item = Office.context.mailbox.item;
    if (item.attachments.length > 0) {
      const promises: any[] = [];
      for (let i: number = 0; i < item.attachments.length; i++) {
        promises.push(
          new Promise<any>((resolve2, reject2) => {
            item.getAttachmentContentAsync(
              item.attachments[i].id,
              { asyncContext: item.attachments[i] },
              (result: Office.AsyncResult<Office.AttachmentContent>) => {
                if (result.status === Office.AsyncResultStatus.Succeeded) {
                  switch (result.value.format) {
                    case Office.MailboxEnums.AttachmentContentFormat.Base64:
                      // Handle file attachment.
                      resolve2({
                        attachmentsDetails: result.asyncContext,
                        attachmentsContent: {
                          type: Office.MailboxEnums.AttachmentContentFormat.Base64,
                          content: this.getBinaryFileContents(result.value.content),
                        },
                      });
                      break;
                    case Office.MailboxEnums.AttachmentContentFormat.Eml:
                      // Handle email item attachment.
                      resolve2({
                        attachmentsDetails: result.asyncContext,
                        attachmentsContent: {
                          type: Office.MailboxEnums.AttachmentContentFormat.Eml,
                          content: 'email',
                        },
                      });
                      break;
                    case Office.MailboxEnums.AttachmentContentFormat.ICalendar:
                      // Handle .icalender attachment.
                      resolve2({
                        attachmentsDetails: result.asyncContext,
                        attachmentsContent: {
                          type: Office.MailboxEnums.AttachmentContentFormat.Eml,
                          content: 'icalender',
                        },
                      });
                      break;
                    case Office.MailboxEnums.AttachmentContentFormat.Url:
                      // Handle cloud attachment.
                      resolve2({
                        attachmentsDetails: result.asyncContext,
                        attachmentsContent: {
                          type: Office.MailboxEnums.AttachmentContentFormat.Url,
                          content: 'URL',
                        },
                      });
                      break;
                    default:
                      resolve2({
                        attachmentsDetails: result.asyncContext,
                        attachmentsContent: undefined,
                      });
                      break;
                  }
                } else {
                  reject2({
                    data: undefined,
                    error: result.error.message,
                  });
                }
              },
            );
          }),
        );
      }
      Promise.all(promises)
        .then((results) => {
          console.log(results);
          resolve({
            data: results,
            error: '',
          });
        })
        .catch((e) => {
          reject({
            data: undefined,
            error: e,
          });
        });
    } else {
      resolve({
        data: [],
        error: '',
      });
    }
  } catch (error) {
    reject({
      data: undefined,
      error: error,
    });
  }
});
Matej
  • 396
  • 1
  • 9
0

Due to the asynchronous nature of the removeAttachmentAsync API, you will need async/await to be sure the API call is complete. If you need to perform some action after each attachment is removed, you can add this code in the callback, which will be called once the API completes.

Another possible workaround would be to use a Polyfill or a library that will provide promises/async/await in IE11. Options for promises in IE are discussed in this Stack Overflow post.