Office.js - item.body is not an instance of Body in Outlook on the Web

50 views Asked by At

I’m developing an Outlook add-in that’s supposed to work with both Outlook on the Web and Outlook 365 for Windows and Mac. I’m encountering an issue with the item.body.prependAsync method. When I try to call this method, I get an error saying item.body.prependAsync is not a function.

Here’s what console.log(item.body) gives me:

{type: undefined, getAsync: ƒ}
getAsync: ƒ wt(e)
type: undefined
[[Prototype]]: Object

Here’s how I’m structuring my html:

<!DOCTYPE html>
<html>

<head>
  <title>Addin test</title>
  <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>
  <script>
    let item;

    Office.onReady((info) => {
      if (info.host === Office.HostType.Outlook) {
        item = Office.context.mailbox.item;
        console.log(item.body);
        item.body.prependAsync("text", function (asyncResult) {
          if (asyncResult.status === Office.AsyncResultStatus.Failed) {
            console.log("Action failed with error: " + asyncResult.error.message);
            return;
          }

          console.log(`"${'text'}" prepended to the body.`);
        });
      }
    });
  </script>
</head>

</html>

I receive that error in my logs:

Uncaught TypeError: item.body.prependAsync is not a function
    at URLtoHTML/html?et=:14:19
    at appsforoffice.microsoft.com/lib/1/hosted/office.js:76:23268
    at t (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:22669)
    at appsforoffice.microsoft.com/lib/1/hosted/office.js:76:29316
    at c (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:4168)
    at f.waitForFunction (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:4257)
    at e.j [as appReadyCallback] (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:28650)
    at appsforoffice.microsoft.com/lib/1/hosted/outlook-web-16.01.js:20:339988

This is my slightly reduced manifest.xml:

<?xml version="1.0" encoding="UTF-8"?>
<OfficeApp
  xmlns="http://schemas.microsoft.com/office/appforoffice/1.1"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0"
  xmlns:mailappor="http://schemas.microsoft.com/office/mailappversionoverrides/1.0"
  xsi:type="MailApp">


  <Id>7218ed19-90f5-4ad6-99e8-8d17ee2556</Id>
  <Version>1.0.0.0</Version>

  <Hosts>
    <Host Name="Mailbox" />
  </Hosts>
  <Requirements>
    <Sets>
      <Set Name="Mailbox" MinVersion="1.1" />
    </Sets>
  </Requirements>
  <FormSettings>
    <Form xsi:type="ItemRead">
      <DesktopSettings>
        <SourceLocation DefaultValue="URL to HTML" />
        <RequestedHeight>250</RequestedHeight>
      </DesktopSettings>
    </Form>
  </FormSettings>

  <Permissions>ReadWriteItem</Permissions>
  <Rule xsi:type="RuleCollection" Mode="Or">
    <Rule xsi:type="ItemIs" ItemType="Message" FormType="Read" />
  </Rule>
  <DisableEntityHighlighting>false</DisableEntityHighlighting>

  <VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides"
    xsi:type="VersionOverridesV1_0">
    <Requirements>
      <bt:Sets DefaultMinVersion="1.1">
        <bt:Set Name="Mailbox" />
      </bt:Sets>
    </Requirements>
    <Hosts>
      <Host xsi:type="MailHost">

        <DesktopFormFactor>
          <FunctionFile resid="Commands.Url" />

          <ExtensionPoint xsi:type="MessageReadCommandSurface">
            <OfficeTab id="TabDefault">
              <Group id="msgReadGroup">
                <Label resid="GroupLabel" />
                <Control xsi:type="Button" id="msgReadOpenPaneButton">
                  <Label resid="TaskpaneButton.Label" />
                  <Supertip>
                    <Title resid="TaskpaneButton.Label" />
                    <Description resid="TaskpaneButton.Tooltip" />
                  </Supertip>
                  <Icon>
                    <bt:Image size="16" resid="Icon.16x16" />
                    <bt:Image size="32" resid="Icon.32x32" />
                    <bt:Image size="80" resid="Icon.80x80" />
                  </Icon>

                </Control>
              </Group>
            </OfficeTab>
          </ExtensionPoint>
        </DesktopFormFactor>
      </Host>
    </Hosts>

    <Resources>
      <bt:Urls>
        <bt:Url id="Commands.Url" DefaultValue="URL to HTML" />
      </bt:Urls>
      <bt:ShortStrings>
        <bt:String id="GroupLabel" DefaultValue="Name" />
        <bt:String id="TaskpaneButton.Label" DefaultValue="Action" />
      </bt:ShortStrings>
    </Resources>
  </VersionOverrides>
</OfficeApp>

I’m testing this in Outlook on the Web at https://outlook.office365.com/. Any idea why item.body might not be the expected Body object? Any help would be greatly appreciated!

2

There are 2 answers

0
Oleg O - MSFT On

This API is only supported during compose, so try using this API with a MessageComposeCommandSurface extension point in your manifest instead.

0
Martin Adams On

Uncaught TypeError: item.body.prependAsync is not a function

Uncaught TypeError: item.body.prependAsync is not a function

Add the MessageComposeCommandSurface extension point

<!-- Message Compose -->
<ExtensionPoint xsi:type="MessageComposeCommandSurface">
  <OfficeTab id="TabDefault">
    <Group id="msgComposeCmdGroup">
      <Label resid="GroupLabel"/>
      <Control xsi:type="Button" id="msgComposeInsertGist">
        <Label resid="TaskpaneButton.Label"/>
        <Supertip>
          <Title resid="TaskpaneButton.Title"/>
          <Description resid="TaskpaneButton.Tooltip"/>
        </Supertip>
        <Icon>
          <bt:Image size="16" resid="Icon.16x16"/>
          <bt:Image size="32" resid="Icon.32x32"/>
          <bt:Image size="80" resid="Icon.80x80"/>
        </Icon>
        <Action xsi:type="ShowTaskpane">
          <SourceLocation resid="Taskpane.Url"/>
        </Action>
      </Control>
      <Control xsi:type="Button" id="msgComposeInsertDefaultGist">
        <Label resid="FunctionButton.Label"/>
        <Supertip>
          <Title resid="FunctionButton.Title"/>
          <Description resid="FunctionButton.Tooltip"/>
        </Supertip>
        <Icon>
          <bt:Image size="16" resid="Icon.16x16"/>
          <bt:Image size="32" resid="Icon.32x32"/>
          <bt:Image size="80" resid="Icon.80x80"/>
        </Icon>
        <Action xsi:type="ExecuteFunction">
          <FunctionName>insertDefaultGist</FunctionName>
        </Action>
      </Control>
    </Group>
  </OfficeTab>
</ExtensionPoint>

ExtensionPoint element

MessageComposeCommandSurface