Why is my Outlook Add-in silently failing the "onMessageSendHandler" action when side-loading from a static website?

huangapple go评论57阅读模式
英文:

Why is my Outlook Add-in silently failing the "onMessageSendHandler" action when side-loading from a static website?

问题

我使用Yeoman生成器创建了一个小的Outlook插件,并在Visual Studio Code中进行开发。

该插件仅执行两项操作:

  1. onNewMessageComposeHandler操作触发时,将电子邮件消息的主题设置为“Hello, World!”。
  2. onMessageSendHandler操作触发时,将电子邮件地址添加到收件人的“To:”列表中。

这是我的launchevents.js文件中的代码:

const bccRecipientEmail = "foo@bar.test";

function onMessageSendHandler(event) {
  setBcc(event);
  event.completed({ allowEvent: true });
}

function onNewMessageComposeHandler(event) {
  setSubject(event);
}

// ... 其他函数

if (Office.context.platform === Office.PlatformType.PC || Office.context.platform == null) {
  Office.actions.associate("onMessageSendHandler", onMessageSendHandler);
  Office.actions.associate("onNewMessageComposeHandler", onNewMessageComposeHandler);
}

这些是我的manifest.xml文件中的相关部分:

<!-- ... 其他部分 ... -->
<ExtensionPoint xsi:type="LaunchEvent">
  <LaunchEvents>
    <LaunchEvent Type="OnNewMessageCompose" FunctionName="onNewMessageComposeHandler"/>
    <LaunchEvent Type="OnMessageSend" FunctionName="onMessageSendHandler" SendMode="PromptUser" />
  </LaunchEvents>
  <!-- Identifies the runtime to be used (also referenced by the Runtime element). -->
  <SourceLocation resid="WebViewRuntime.Url"/>
</ExtensionPoint>
<!-- ... 其他部分 ... -->

当我在我的计算机上运行插件并将其侧向加载到Outlook on Windows1时,它运行正常。我可以看到在撰写邮件时主题行会更改,发送邮件时确实会将电子邮件地址添加到收件人列表中。

现在我正在尝试将插件发布到Azure存储的静态站点,按照文档中的说明进行操作。当我从静态站点的URL侧向加载插件时,我发现主题行成功更改,但确实未将电子邮件地址添加到收件人列表中。

我的问题是 - 为什么当我从静态站点侧向加载插件时,onMessageSendHandler不按预期工作?

1 Outlook版本:Microsoft® Outlook® for Microsoft 365 MSO(版本2304 Build 16.0.16327.20200)64位

英文:

I've created a small Outlook Add-in using the Yeoman generator and I've developing it using Visual Studio Code.

The add-in only does two things:

  1. When the onNewMessageComposeHandler action fires, it sets the email message's subject line to "Hello, World!"
  2. When the onMessageSendHandler action fires, it adds an email address to the To: list of recipients.

This is the code in my launchevents.js file:

const bccRecipientEmail = &quot;foo@bar.test&quot;;

function onMessageSendHandler(event) {
  setBcc(event);
  event.completed({ allowEvent: true });
}

function onNewMessageComposeHandler(event) {
  setSubject(event);
}

function setBcc(event) {
  Office.context.mailbox.item.bcc.addAsync(
    [bccRecipientEmail],
    {
      asyncContext: event,
    },
    function (asyncResult) {
      // Handle success or error.
      if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
        console.error(&quot;Failed to set bcc to &#39;&quot; + bccRecipientEmail + &quot;&#39;: &quot; + JSON.stringify(asyncResult.error));
      }

      // Call event.completed() after all work is done.
      asyncResult.asyncContext.completed();
    }
  );
}

function setSubject(event) {
  Office.context.mailbox.item.subject.setAsync(
    &quot;Set by an event-based add-in! WITH COMPOSE&quot;,
    {
      asyncContext: event,
    },
    function (asyncResult) {
      // Handle success or error.
      if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
        console.error(&quot;Failed to set subject: &quot; + JSON.stringify(asyncResult.error));
      }

      // Call event.completed() after all work is done.
      asyncResult.asyncContext.completed();
    }
  );
}

if (Office.context.platform === Office.PlatformType.PC || Office.context.platform == null) {
  Office.actions.associate(&quot;onMessageSendHandler&quot;, onMessageSendHandler);
  Office.actions.associate(&quot;onNewMessageComposeHandler&quot;, onNewMessageComposeHandler);
}

These are the relevant parts from my manifest.xml file:

&lt;VersionOverrides xmlns=&quot;http://schemas.microsoft.com/office/mailappversionoverrides&quot; xsi:type=&quot;VersionOverridesV1_0&quot;&gt;
  &lt;VersionOverrides xmlns=&quot;http://schemas.microsoft.com/office/mailappversionoverrides/1.1&quot; xsi:type=&quot;VersionOverridesV1_1&quot;&gt;
    &lt;Requirements&gt;
      &lt;bt:Sets DefaultMinVersion=&quot;1.12&quot;&gt;
        &lt;bt:Set Name=&quot;Mailbox&quot; /&gt;
      &lt;/bt:Sets&gt;
    &lt;/Requirements&gt;
    &lt;Hosts&gt;
      &lt;Host xsi:type=&quot;MailHost&quot;&gt;
        &lt;!-- Event-based activation happens in a lightweight runtime.--&gt;
        &lt;Runtimes&gt;
          &lt;!-- HTML file including reference to or inline JavaScript event handlers.
               This is used by Outlook on the web and Outlook on the new Mac UI. --&gt;
          &lt;Runtime resid=&quot;WebViewRuntime.Url&quot;&gt;
            &lt;!-- JavaScript file containing event handlers. This is used by Outlook on Windows. --&gt;
            &lt;Override type=&quot;javascript&quot; resid=&quot;JSRuntime.Url&quot;/&gt;
          &lt;/Runtime&gt;
        &lt;/Runtimes&gt;
        &lt;DesktopFormFactor&gt;
          &lt;FunctionFile resid=&quot;Commands.Url&quot; /&gt;
          &lt;ExtensionPoint xsi:type=&quot;MessageReadCommandSurface&quot;&gt;
		    [...]
          &lt;/ExtensionPoint&gt;

          &lt;!-- Can configure other command surface extension points for add-in command support. --&gt;

          &lt;!-- Enable launching the add-in on the included events. --&gt;
          &lt;ExtensionPoint xsi:type=&quot;LaunchEvent&quot;&gt;
            &lt;LaunchEvents&gt;
              &lt;LaunchEvent Type=&quot;OnNewMessageCompose&quot; FunctionName=&quot;onNewMessageComposeHandler&quot;/&gt;
              &lt;LaunchEvent Type=&quot;OnMessageSend&quot; FunctionName=&quot;onMessageSendHandler&quot; SendMode=&quot;PromptUser&quot; /&gt;
            &lt;/LaunchEvents&gt;
            &lt;!-- Identifies the runtime to be used (also referenced by the Runtime element). --&gt;
            &lt;SourceLocation resid=&quot;WebViewRuntime.Url&quot;/&gt;
          &lt;/ExtensionPoint&gt;
        &lt;/DesktopFormFactor&gt;
      &lt;/Host&gt;
    &lt;/Hosts&gt;
    &lt;Resources&gt;
      &lt;bt:Images&gt;
        &lt;bt:Image id=&quot;Icon.16x16&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/assets/icon-16.png&quot;/&gt;
        &lt;bt:Image id=&quot;Icon.32x32&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/assets/icon-32.png&quot;/&gt;
        &lt;bt:Image id=&quot;Icon.80x80&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/assets/icon-80.png&quot;/&gt;
      &lt;/bt:Images&gt;
      &lt;bt:Urls&gt;
        &lt;bt:Url id=&quot;Commands.Url&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/commands.html&quot; /&gt;
        &lt;bt:Url id=&quot;Taskpane.Url&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/taskpane.html&quot; /&gt;
        &lt;bt:Url id=&quot;WebViewRuntime.Url&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/commands.html&quot; /&gt;
        &lt;!-- Entry needed for Outlook on Windows. --&gt;
        &lt;bt:Url id=&quot;JSRuntime.Url&quot; DefaultValue=&quot;https://[REDACTED].web.core.windows.net/launchevent.js&quot; /&gt;
      &lt;/bt:Urls&gt;
      &lt;bt:ShortStrings&gt;
        &lt;bt:String id=&quot;GroupLabel&quot; DefaultValue=&quot;Sync Emails to Quickbase&quot;/&gt;
        &lt;bt:String id=&quot;TaskpaneButton.Label&quot; DefaultValue=&quot;Show Taskpane&quot;/&gt;
        &lt;bt:String id=&quot;ActionButton.Label&quot; DefaultValue=&quot;Perform an action&quot;/&gt;
      &lt;/bt:ShortStrings&gt;
      &lt;bt:LongStrings&gt;
        &lt;bt:String id=&quot;TaskpaneButton.Tooltip&quot; DefaultValue=&quot;Opens a pane displaying all available properties.&quot;/&gt;
        &lt;bt:String id=&quot;ActionButton.Tooltip&quot; DefaultValue=&quot;Perform an action when clicked.&quot;/&gt;
      &lt;/bt:LongStrings&gt;
    &lt;/Resources&gt;
  &lt;/VersionOverrides&gt;
&lt;/VersionOverrides&gt;

When I run the add-in on my machine and side-load it into Outlook on Windows<sup>1</sup>, it works well. I can see the subject line change when I compose the message and when I send it, the email address is indeed added to the recipients list.

Now I'm trying to publish my add-in to a static site in Azure Storage, as per the documentation. When I side-load the add-in from the static site's URL I find that the subject line changes successfully but the email address is not added to the list of recipients.

My question is - Why, when I side-load the add-in from a static site, does the onMessageSendHandler not work as expected?


<sup>1</sup> Outlook version: Microsoft® Outlook® for Microsoft 365 MSO (Version 2304 Build 16.0.16327.20200) 64-bit

答案1

得分: 1

尝试向事件处理程序添加任何日志记录语句,以便您可以确定特定事件处理程序是否被调用。

确保您的代码包括以下部分:

// 重要提示:为了确保您的加载项在 Windows 上的 Outlook 客户端中受支持,请记住将清单中的 LaunchEvent 元素中指定的事件处理程序名称映射到其 JavaScript 对应项。
// 第一个参数:清单中的 LaunchEvent 的 FunctionName;第二个参数:此 .js 文件中的实现。
if (Office.context.platform === Office.PlatformType.PC || Office.context.platform == null) {
  Office.actions.associate("onNewMessageComposeHandler", onNewMessageComposeHandler);
  Office.actions.associate("OnMessageSend", onMessageSendHandler);
}

在运行在 Windows 上的 Outlook 中运行基于事件的加载项不会运行包含在 Office.onReady()Office.initialize 函数中的代码。我们建议将加载项的启动逻辑,如检查用户的 Outlook 版本,添加到您的事件处理程序中。

有关更多信息,请参阅实现事件处理

还请尝试找到加载项的 bundle.js 文件,导航到以下文件夹中的 File Explorer。将方括号中的文本替换为适用于您的 Outlook 和加载项信息。

%LOCALAPPDATA%\Microsoft\Office.0\Wef\{[Outlook profile GUID]}\[Outlook mail account encoding]\Javascript\[Add-in ID]_[Add-in Version]_[locale]

在 Visual Studio Code 中打开 bundle.js 并检查源代码。

在源代码中,我注意到 completed 方法是从 setAsync 回调中调用的。这根本不需要。有关如何设置收件人的更多信息,请参阅在 Outlook 中撰写约会或消息时获取、设置或添加收件人

英文:

Try to add any logging statements to the events handlers, so you could see whether a particular event handler is invoked for sure.

Make sure that your code includes the following piece:

// IMPORTANT: To ensure your add-in is supported in the Outlook client on Windows, remember to map the event handler name specified in the manifest&#39;s LaunchEvent element to its JavaScript counterpart.
// 1st parameter: FunctionName of LaunchEvent in the manifest; 2nd parameter: Its implementation in this .js file.
if (Office.context.platform === Office.PlatformType.PC || Office.context.platform == null) {
  Office.actions.associate(&quot;onNewMessageComposeHandler&quot;, onNewMessageComposeHandler);
  Office.actions.associate(&quot;OnMessageSend&quot;, onMessageSendHandler);
}

Event-based add-ins running in Outlook on Windows don't run code included in the Office.onReady() and Office.initialize functions. We recommend adding your add-in startup logic, such as checking the user's Outlook version, to your event handlers instead.

See Implement event handling for more information about that.

Also try to find the add-in's bundle.js file, navigate to the following folder in File Explorer. Replace text enclosed in [] with your applicable Outlook and add-in information.

%LOCALAPPDATA%\Microsoft\Office.0\Wef\{[Outlook profile GUID]}\[Outlook mail account encoding]\Javascript\[Add-in ID]_[Add-in Version]_[locale]

Open bundle.js in Visual Studio Code and check the source code.


In the source code I've noticed that the completed method is called from the setAsync callback. That is not required at all. See Get, set, or add recipients when composing an appointment or message in Outlook for more information how to set up recipients.

huangapple
  • 本文由 发表于 2023年5月28日 21:53:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定