javascriptoutlookoffice-jsoutlook-addinoutlook-web-addins

Error calling Office.context.mailbox.item.body.getTypeAsync when reading an Outlook message


I'm creating my first Outlook Web Add-in app. When I'm calling Office.context.mailbox.item.body.getTypeAsync when viewing an email in Outlook, I get the error message "Uncaught TypeError: Office.context.mailbox.item.body.getTypeAsync is not a function":

(function () {
    Office.onReady(function () {
        Office.context.mailbox.item.body.getTypeAsync((typeResult) => {
            if (typeResult.status === Office.AsyncResultStatus.Succeeded) {
                const coercionType = typeResult.value;

                Office.context.mailbox.item.body.getAsync(coercionType, (getResult) => {
                    if (getResult.status === Office.AsyncResultStatus.Succeeded) {
                        // Do something with getResult.value - message body content.
                    }
                    else {
                        console.log(getResult.error.message);
                    }
                });
            }
            else {
                console.log(typeResult.error.message);
            }
        });
    });
})();

If I separate out the code and call Office.context.mailbox.item.body.getAsync with a hard coded coercion type (Office.CoercionType.Html or Office.CoercionType.Text), getAsync works as expected. But getTypeAsync is not available on Office.context.mailbox.item.body as a function, with getAsync being the sole function on Office.context.mailbox.item.body when reading the message in Outlook web and inspecting using dev tools. This seems unusual, as other functions should be available on Office.context.mailbox.item.body as indicated by the Office.js documentation.

What could be causing the problem, and how do I fix it so that I can call getTypeAsync correctly? I am using the live Office.js framework from Microsoft, so that should include all of the latest functionality. Is it likely to be an issue with the manifest file, or an issue of getting the Office.js framework from Microsoft?

Here is part of the header in my MessageRead.html base file with reference to my JavaScript files, including the Office.js framework:

<script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
<script src="MessageRead.js" type="module"></script>

Solution

  • As @Slava Ivanov said this Office.context.mailbox.item.body.getTypeAsync is available in Compose Mode mean when you're creating a new email.

    If you want to get email body in read mode you need to give its body type by yourself.

    for example get body in HTML Format:

    Office.context.mailbox.item.body.getAsync(
        Office.CoercionType.Html,
        { asyncContext: "This is passed to the callback" },
        function callback(result) {
          // Do something with the result.
          console.log(result.value) //body in HTML Format 
        });
    

    If you need mail body in text Text Format Just change its CoercionType Html to Text

    For example:

    Office.context.mailbox.item.body.getAsync(
        Office.CoercionType.Text,
        { asyncContext: "This is passed to the callback" },
        function callback(result) {
          // Do something with the result.
          console.log(result.value)   // body in text format 
        });
    

    Documentation Office.Body interface Method: getAsync