skip to Main Content

Requirement:
Get email subject from outlook inbox by outlook add-in API.

Environment:

  1. Outlook add-in (sideload)
  2. "Mailbox" MinVersion="1.1"
  3. Visual studio code

Code:

async function ReadEmailSubject() {
  document.getElementById("email-subject").innerText = ""; 
  Office.context.mailbox.item.subject.getAsync(function (result) {
    var emailSubjectDiv = document.getElementById("email-subject");
    if (result.status === Office.AsyncResultStatus.Succeeded) {

        var emailSubject = result.value;
        emailSubjectDiv.innerText = emailSubject;
    } else {

        console.error("Failed to get email subject: " + result.error.message);
        emailSubjectDiv.innerText = "Failed to get email body: " + result.error.message;
    }
  });
}

Debug console:

TypeError: Office.context.mailbox.item.subject.getAsync is not a function
    at _callee2$ (e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:28:39)
    at tryCatch (e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1)
    at Generator.<anonymous> (e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1)
    at Generator.next (e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1)
    at asyncGeneratorStep (e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1)
    at _next (e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1)
    at e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1
    at new Promise (<anonymous>)
    at e:office-add-insOutlookyo-mainOutlook Add-insrctaskpanetaskpane.js:2:1
    at _ReadEmailSubject (https://localhost:3000/taskpane.js:266:28) {stack: 'TypeError: Office.context.mailbox.item.subjec…t (https://localhost:3000/taskpane.js:266:28)', message: 'Office.context.mailbox.item.subject.getAsync is not a function'}

What I have tried:

  1. change "Mailbox" MinVersion="1.1"
  2. use "Office.context.mailbox.item.subject.getAsync({asyncContext: "Getting email subject" },function (result){…})"
    The error still appeared.

2

Answers


  1. It is not clear where and when the ReadEmailSubject is called in the add-in. But the following code works fine on my machine in Outlook for Desktop:

    let item;
    
    Office.initialize = function () {
        item = Office.context.mailbox.item;
        // Checks for the DOM to load using the jQuery ready method.
        $(document).ready(function () {
            // After the DOM is loaded, app-specific code can run.
            // Get the subject of the item being composed.
            getSubject();
        });
    }
    
    // Get the subject of the item that the user is composing.
    function getSubject() {
        item.subject.getAsync(
            function (asyncResult) {
                if (asyncResult.status == Office.AsyncResultStatus.Failed){
                    write(asyncResult.error.message);
                }
                else {
                    // Successfully got the subject, display it.
                    write ('The subject is: ' + asyncResult.value);
                }
            });
    }
    
    // Write to a div with id='message' on the page.
    function write(message){
        document.getElementById('message').innerText += message; 
    }
    

    You can find a bunch of sample Outlook add-ins to verify what is wrong with your code, for example, see https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Samples/outlook-verify-sensitivity-label .

    Login or Signup to reply.
  2. This API is available in compose scenarios only. You will get this error if you try to use this API in read scenarios.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search