Automatically set the subject of a new message or appointment
Artikel
Need to add a required disclaimer to all your messages? With an event-based add-in, content is automatically added to new messages or appointments. Your users can focus on writing, instead of compliance.
The following sections teach you how to develop an add-in that handles the OnNewMessageCompose and OnNewAppointmentOrganizer events. By the end of this walkthrough, you'll have an add-in that automatically sets the subject of new messages and appointments being created.
Navigate to the "authorization.permissions.resourceSpecific" array. In the array object, replace the value of the "name" property with "MailboxItem.ReadWrite.User". This is needed by the add-in to be able to set the subject of the mail item.
Add the following object to the "extensions.runtimes" array. Note the following about this markup:
The "minVersion" of the Mailbox requirement set is configured to "1.10" as this is the lowest version of the requirement set that supports the OnNewMessageCompose and OnNewAppointmentOrganizer events.
The "id" of the runtime is set to the descriptive name "autorun_runtime".
The "code" property has a child "page" property that is set to an HTML file and a child "script" property that is set to a JavaScript file. You'll create or edit these files in later steps. Office uses one of these values depending on the platform.
Office on Windows executes the event handlers in a JavaScript-only runtime, which loads a JavaScript file directly.
Office on Mac and on the web, and new Outlook on Windows execute the handlers in a browser runtime, which loads an HTML file. That file, in turn, contains a <script> tag that loads the JavaScript file.
The "lifetime" property is set to "short", which means that the runtime starts up when one of the events is triggered and shuts down when the handler completes. (In certain rare cases, the runtime shuts down before the handler completes. See Runtimes in Office Add-ins.)
There are two types of "actions" that can run in the runtime. You'll create functions to correspond to these actions in a later step.
Add the following "autoRunEvents" array as a property of the object in the "extensions" array.
JSON
"autoRunEvents": [
]
Add the following object to the "autoRunEvents" array. The "events" property maps handlers to events as described in the table earlier in this article. The handler names must match those used in the "id" properties of the objects in the "actions" array in an earlier step.
To enable event-based activation of your add-in, you must configure the Runtimes element and LaunchEvent extension point in the VersionOverridesV1_1 node of the manifest.
In event-based add-ins, classic Outlook on Windows uses a JavaScript file, while Outlook on the web and on the new Mac UI, and new Outlook on Windows use an HTML file that can reference the same JavaScript file. You must provide references to both these files in the Resources node of the manifest as the Outlook platform ultimately determines whether to use HTML or JavaScript based on the Outlook client. As such, to configure event handling, provide the location of the HTML in the <Runtime> element, then in its Override child element provide the location of the JavaScript file inlined or referenced by the HTML.
In your code editor, open the quick start project.
Open the manifest.xml file located at the root of your project.
Select the entire <VersionOverrides> node (including open and close tags) and replace it with the following XML, then save your changes.
XML
<VersionOverridesxmlns="http://schemas.microsoft.com/office/mailappversionoverrides"xsi:type="VersionOverridesV1_0"><VersionOverridesxmlns="http://schemas.microsoft.com/office/mailappversionoverrides/1.1"xsi:type="VersionOverridesV1_1"><Requirements><bt:SetsDefaultMinVersion="1.10"><bt:SetName="Mailbox" /></bt:Sets></Requirements><Hosts><Hostxsi:type="MailHost"><!-- Event-based activation happens in a lightweight runtime.--><Runtimes><!-- HTML file including reference to or inline JavaScript event handlers.
This is used by Outlook on the web and on the new Mac UI, and new Outlook on Windows. --><Runtimeresid="WebViewRuntime.Url"><!-- JavaScript file containing event handlers. This is used by classic Outlook on Windows. --><Overridetype="javascript"resid="JSRuntime.Url"/></Runtime></Runtimes><DesktopFormFactor><FunctionFileresid="Commands.Url" /><ExtensionPointxsi:type="MessageReadCommandSurface"><OfficeTabid="TabDefault"><Groupid="msgReadGroup"><Labelresid="GroupLabel" /><Controlxsi:type="Button"id="msgReadOpenPaneButton"><Labelresid="TaskpaneButton.Label" /><Supertip><Titleresid="TaskpaneButton.Label" /><Descriptionresid="TaskpaneButton.Tooltip" /></Supertip><Icon><bt:Imagesize="16"resid="Icon.16x16" /><bt:Imagesize="32"resid="Icon.32x32" /><bt:Imagesize="80"resid="Icon.80x80" /></Icon><Actionxsi:type="ShowTaskpane"><SourceLocationresid="Taskpane.Url" /></Action></Control><Controlxsi:type="Button"id="ActionButton"><Labelresid="ActionButton.Label"/><Supertip><Titleresid="ActionButton.Label"/><Descriptionresid="ActionButton.Tooltip"/></Supertip><Icon><bt:Imagesize="16"resid="Icon.16x16"/><bt:Imagesize="32"resid="Icon.32x32"/><bt:Imagesize="80"resid="Icon.80x80"/></Icon><Actionxsi:type="ExecuteFunction"><FunctionName>action</FunctionName></Action></Control></Group></OfficeTab></ExtensionPoint><!-- Can configure other command surface extension points for add-in command support. --><!-- Enable launching the add-in on the included events. --><ExtensionPointxsi:type="LaunchEvent"><LaunchEvents><LaunchEventType="OnNewMessageCompose"FunctionName="onNewMessageComposeHandler"/><LaunchEventType="OnNewAppointmentOrganizer"FunctionName="onNewAppointmentComposeHandler"/></LaunchEvents><!-- Identifies the runtime to be used (also referenced by the Runtime element). --><SourceLocationresid="WebViewRuntime.Url"/></ExtensionPoint></DesktopFormFactor></Host></Hosts><Resources><bt:Images><bt:Imageid="Icon.16x16"DefaultValue="https://localhost:3000/assets/icon-16.png"/><bt:Imageid="Icon.32x32"DefaultValue="https://localhost:3000/assets/icon-32.png"/><bt:Imageid="Icon.80x80"DefaultValue="https://localhost:3000/assets/icon-80.png"/></bt:Images><bt:Urls><bt:Urlid="Commands.Url"DefaultValue="https://localhost:3000/commands.html" /><bt:Urlid="Taskpane.Url"DefaultValue="https://localhost:3000/taskpane.html" /><bt:Urlid="WebViewRuntime.Url"DefaultValue="https://localhost:3000/commands.html" /><!-- Entry needed for classic Outlook on Windows. --><bt:Urlid="JSRuntime.Url"DefaultValue="https://localhost:3000/launchevent.js" /></bt:Urls><bt:ShortStrings><bt:Stringid="GroupLabel"DefaultValue="Contoso Add-in"/><bt:Stringid="TaskpaneButton.Label"DefaultValue="Show Taskpane"/><bt:Stringid="ActionButton.Label"DefaultValue="Perform an action"/></bt:ShortStrings><bt:LongStrings><bt:Stringid="TaskpaneButton.Tooltip"DefaultValue="Opens a pane displaying all available properties."/><bt:Stringid="ActionButton.Tooltip"DefaultValue="Perform an action when clicked."/></bt:LongStrings></Resources></VersionOverrides></VersionOverrides>
From the same quick start project, create a new folder named launchevent under the ./src directory.
In the ./src/launchevent folder, create a new file named launchevent.js.
Open the file ./src/launchevent/launchevent.js in your code editor and add the following JavaScript code.
JavaScript
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/functiononNewMessageComposeHandler(event) {
setSubject(event);
}
functiononNewAppointmentComposeHandler(event) {
setSubject(event);
}
functionsetSubject(event) {
Office.context.mailbox.item.subject.setAsync(
"Set by an event-based add-in!",
{
"asyncContext": event
},
function (asyncResult) {
// Handle success or error.if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
console.error("Failed to set subject: " + JSON.stringify(asyncResult.error));
}
// Call event.completed() to signal to the Outlook client that the add-in has completed processing the event.
asyncResult.asyncContext.completed();
});
}
// IMPORTANT: To ensure your add-in is supported in Outlook, remember to map the event handler name specified in the manifest to its JavaScript counterpart.
Office.actions.associate("onNewMessageComposeHandler", onNewMessageComposeHandler);
Office.actions.associate("onNewAppointmentComposeHandler", onNewAppointmentComposeHandler);
Open the webpack.config.js file found in the root directory of the project and complete the following steps.
Locate the plugins array within the config object and add this new object at the beginning of the array.
JavaScript
new CopyWebpackPlugin({
patterns: [
{
from: "./src/launchevent/launchevent.js",
to: "launchevent.js",
},
],
}),
Save your changes.
Try it out
Run the following commands in the root directory of your project. When you run npm start, the local web server will start (if it's not already running) and your add-in will be sideloaded.
command line
npm run build
command line
npm start
Catatan
If your add-in wasn't automatically sideloaded, follow the instructions in Sideload Outlook add-ins for testing to manually sideload the add-in in Outlook.
In Outlook on the web or in new Outlook on Windows, create a new message.
In Outlook on the new Mac UI, create a new message.
In classic Outlook on Windows, create a new message.
When you want to stop the local web server and uninstall the add-in, follow the applicable instructions:
To stop the server, run the following command. If you used npm start, the following command should also uninstall the add-in.
Sumber untuk konten ini dapat ditemukan di GitHub, yang juga dapat Anda gunakan untuk membuat dan meninjau masalah dan menarik permintaan. Untuk informasi selengkapnya, lihat panduan kontributor kami.
Umpan balik Office Add-ins
Office Add-ins adalah proyek sumber terbuka. Pilih tautan untuk memberikan umpan balik:
Tunjukkan bahwa Anda memiliki keterampilan yang diperlukan untuk mendapatkan hasil maksimal dari Outlook 2019 dengan mendapatkan sertifikasi Microsoft Office Specialist (MOS).