Requirement:
Get email subject from outlook inbox by outlook add-in API.
Environment:
- Outlook add-in (sideload)
- "Mailbox" MinVersion="1.1"
- 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:
- change "Mailbox" MinVersion="1.1"
- use "Office.context.mailbox.item.subject.getAsync({asyncContext: "Getting email subject" },function (result){…})"
The error still appeared.
2
Answers
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: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 .
This API is available in compose scenarios only. You will get this error if you try to use this API in read scenarios.