How to create Outlook Add-in with Angular 2+?


Isn't it fun,

to instantly listen to a blog on the go? PLAY !

 
 

with-Angular-2

In this blog, we will explain the process of building an Outlook task pan add-in that displays at least one property of a selected message.

With Outlook add-ins we can use ordinary web technologies like HTML, CSS, and JavaScript to build a solution that can run in Outlook throughout multiple platforms, as well as on the web, Windows, iOS, and Mac.

Table of Content

Create the add-in

You can create an office Add-in through the Yeoman generator for Office add-ins or Visual Studio. The Yeoman generator generates or creates a Node.js project which may be managed with VS Code (Visual Studio Code) or any other editor, while Visual Studio creates a Visual Studio solution. Select the tab for the one you want to use and then follow the directions of use to create your add-in and test it locally.

Requirements

  1. The most recent LTS version of Node.js
  2. The most recent version of Yeoman generator and the Yeoman for Office add-ins.

To install these globally, run the following command using the command prompt:

npm install -g yo generator-office

Note: Still if you have previously installed the Yeoman generator, we prefer you update your package to the latest version from npm.

Create the add-in project

Run the below-mentioned command to create an add-in project through theYeoman generator:

yo office

Note:

Once prompted, give the following details to create your add-in project:

  1. Select a project type - Office Aa-in Task Pane project
  2. Select a script type - JavaScript
  3. What do you want to name your add-in? - My OutlookDemo add-in
  4. Which office client application would you like to support - Outlook

Fig: Command Prompt Setup Wizard for add-in project

Once you complete the wizard, the generator will create the project and install supporting Node components.

Tip:You can neglect the guidance of the next step that the Yeoman generator offers after the add-in project's been created. The step by step instructions inside this blog provides all of the guidance you will require to complete this demo.

Navigate to the source folder of the web application project.

cd "My OutlookDemo add-in"

Explore the project

The add-in project that you have created with the Yeoman generator having a sample code for a very basic pane add-in.

  1. The ./manifest.xml file in the root directory of the project specifies the settings and capabilities of the add-in.
  2. The ./src/taskpane/taskpane.html file holds the HTML markup for the task pane.
  3. The ./src/taskpane/taskpane.css file holds the CSS that is applied to content in the task pane.
  4. The ./src/taskpane/taskpane.js file holds the Office JavaScript API code that makes it easier to interact among the Outlook and task pane.

Update the code

In your VSCode or any code editor, open the file ./src/taskpane/taskpane.html and change the whole element with the below-mentioned markup. This new markup includes a label where ever the script in ./src/taskpane/taskpane.js will write data.

<main class="ms-welcome__main" id="app-body" style="display: none;"><h2 class="ms-font-xl"> Identify what Office Add-ins can do for you today!</h2>
<p></p>
<div class="ms-welcome__action ms-Button ms-Button--hero ms-font-xl" id="run" role="button">
            <span class="ms-Button-label">Run</span></div></main>

In your VSCode or any code editor, open the file ./src/taskpane/taskpane.js and include the following code inside the run function. This code will get a reference to the existing message using the Office JavaScript API and write its subject property value to the task pane.


export async function run() {
  // for Get a reference to the current message
  var item = Office.context.mailbox.item;
  // for Write message property value to the task pane
  document.getElementById(&quot;item-subject&quot;).innerHTML = &quot;<b>Subject:</b> 
&quot; + item.subject;
}

Try it out

Notice that Office Add-ins need to use HTTPS, not HTTP, also when you are developing. If you are prompted to install a certificate after you run the below-mentioned command, accept the prompt to install the certificate that the Yeoman generator offers. You can also have to run your command prompt or terminal as an administrator for change to be done.

Run the below-mentioned command in the root directory of your project. Whenever you run this command, the local web server will start if it is not running already.

npm run dev-server

After run dev-server the manifest.xml code looks like the following:


<?xml encoding="UTF-8" standalone="yes" version="1.0">
<officeapp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" xmlns:mailappor="http://schemas.microsoft.com/office/mailappversionoverrides/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MailApp">
  <id>8b211c93-f677-4d2c-a711-4d1db3d9eaff</id>
  <version>1.0.0.0</version>
  <providername>Contoso</providername>
  <defaultlocale>en-US</defaultlocale>
  <displayname defaultvalue="My OutlookDemo add-in" />
  <description defaultvalue="A template to get started." />
  <iconurl defaultvalue="https://localhost:3000/assets/icon-64.png" />
  <highresolutioniconurl defaultvalue="https://localhost:3000/assets/icon-128.png" />
  <supporturl defaultvalue="https://www.contoso.com/help" />
  <appdomains>
    <appdomain>https://www.contoso.com</appdomain>
  </appdomains>
  <hosts>
    <host name="Mailbox" />
  </hosts>
  <requirements>
    <sets>
      <set minversion="1.1" name="Mailbox" />
    </sets>
  </requirements>
  <formsettings><form xsi:type="ItemRead">
      <desktopsettings>
        <sourcelocation defaultvalue="https://localhost:3000/taskpane.html" />
        <requestedheight>250</requestedheight>
      </desktopsettings></form>
  </formsettings>
  <permissions>ReadWriteItem</permissions>
  <rule mode="Or" xsi:type="RuleCollection">
    <rule formtype="Read" itemtype="Message" xsi:type="ItemIs" />
  </rule>
  <disableentityhighlighting>false</disableentityhighlighting>
  <versionoverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0">
    <requirements>
      <bt:sets defaultminversion="1.3">
        <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 id="msgReadOpenPaneButton" xsi:type="Button">
                  <label resid="TaskpaneButton.Label">
                  <supertip><title resid="TaskpaneButton.Label"><description resid="TaskpaneButton.Tooltip" /></title></supertip>
                  <icon>
                    <bt:image resid="Icon.16x16" size="16" />
                    <bt:image resid="Icon.32x32" size="32" />
                    <bt:image resid="Icon.80x80" size="80" />
                  </icon>
                  <action xsi:type="ShowTaskpane">
                    <sourcelocation resid="Taskpane.Url" />
                  </action>
                </label></control><label resid="TaskpaneButton.Label">
                <control id="ActionButton" xsi:type="Button">
                  <label resid="ActionButton.Label">
                  <supertip><title resid="ActionButton.Label"><description resid="ActionButton.Tooltip" /></title></supertip>
                  <icon>
                    <bt:image resid="Icon.16x16" size="16" />
                    <bt:image resid="Icon.32x32" size="32" />
                    <bt:image resid="Icon.80x80" size="80" />
                  </icon>
                  <action xsi:type="ExecuteFunction">
                    <functionname>action</functionname>
                  </action>
                </label></control><label resid="ActionButton.Label">
              </label></label></label></group><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
            </label></label></label></officetab><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
          </label></label></label></extensionpoint><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
        </label></label></label></desktopformfactor><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
      </label></label></label></host><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
    </label></label></label></hosts><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
    <resources>
      <bt:images>
        <bt:image defaultvalue="https://localhost:3000/assets/icon-16.png" id="Icon.16x16" />
        <bt:image defaultvalue="https://localhost:3000/assets/icon-32.png" id="Icon.32x32" />
        <bt:image defaultvalue="https://localhost:3000/assets/icon-80.png" id="Icon.80x80" />
      </bt:images>
      <bt:urls>
        <bt:url defaultvalue="https://localhost:3000/commands.html" id="Commands.Url" />
        <bt:url defaultvalue="https://localhost:3000/taskpane.html" id="Taskpane.Url" />
      </bt:urls>
      <bt:shortstrings>
        <bt:string defaultvalue="Contoso Add-in" id="GroupLabel" />
        <bt:string defaultvalue="Show Taskpane" id="TaskpaneButton.Label" />
        <bt:string defaultvalue="Perform an action" id="ActionButton.Label" />
      </bt:shortstrings>
      <bt:longstrings>
        <bt:string defaultvalue="Opens a pane displaying all available properties." id="TaskpaneButton.Tooltip" />
        <bt:string defaultvalue="Perform an action when clicked." id="ActionButton.Tooltip" />
      </bt:longstrings>
    </resources>
  </label></label></label></versionoverrides><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
</label></label></label></officeapp><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">

</label></label></label></?xml>

with-Angular-2

In this blog, we will explain the process of building an Outlook task pan add-in that displays at least one property of a selected message.

With Outlook add-ins we can use ordinary web technologies like HTML, CSS, and JavaScript to build a solution that can run in Outlook throughout multiple platforms, as well as on the web, Windows, iOS, and Mac.

Table of Content

Create the add-in

You can create an office Add-in through the Yeoman generator for Office add-ins or Visual Studio. The Yeoman generator generates or creates a Node.js project which may be managed with VS Code (Visual Studio Code) or any other editor, while Visual Studio creates a Visual Studio solution. Select the tab for the one you want to use and then follow the directions of use to create your add-in and test it locally.

Requirements

  1. The most recent LTS version of Node.js
  2. The most recent version of Yeoman generator and the Yeoman for Office add-ins.

To install these globally, run the following command using the command prompt:

npm install -g yo generator-office

Note: Still if you have previously installed the Yeoman generator, we prefer you update your package to the latest version from npm.

Create the add-in project

Run the below-mentioned command to create an add-in project through theYeoman generator:

yo office

Note:

Once prompted, give the following details to create your add-in project:

  1. Select a project type - Office Aa-in Task Pane project
  2. Select a script type - JavaScript
  3. What do you want to name your add-in? - My OutlookDemo add-in
  4. Which office client application would you like to support - Outlook

Fig: Command Prompt Setup Wizard for add-in project

Once you complete the wizard, the generator will create the project and install supporting Node components.

Tip:You can neglect the guidance of the next step that the Yeoman generator offers after the add-in project's been created. The step by step instructions inside this blog provides all of the guidance you will require to complete this demo.

Navigate to the source folder of the web application project.

cd "My OutlookDemo add-in"

Explore the project

The add-in project that you have created with the Yeoman generator having a sample code for a very basic pane add-in.

  1. The ./manifest.xml file in the root directory of the project specifies the settings and capabilities of the add-in.
  2. The ./src/taskpane/taskpane.html file holds the HTML markup for the task pane.
  3. The ./src/taskpane/taskpane.css file holds the CSS that is applied to content in the task pane.
  4. The ./src/taskpane/taskpane.js file holds the Office JavaScript API code that makes it easier to interact among the Outlook and task pane.

Update the code

In your VSCode or any code editor, open the file ./src/taskpane/taskpane.html and change the whole element with the below-mentioned markup. This new markup includes a label where ever the script in ./src/taskpane/taskpane.js will write data.

<main class="ms-welcome__main" id="app-body" style="display: none;"><h2 class="ms-font-xl"> Identify what Office Add-ins can do for you today!</h2>
<p></p>
<div class="ms-welcome__action ms-Button ms-Button--hero ms-font-xl" id="run" role="button">
            <span class="ms-Button-label">Run</span></div></main>

In your VSCode or any code editor, open the file ./src/taskpane/taskpane.js and include the following code inside the run function. This code will get a reference to the existing message using the Office JavaScript API and write its subject property value to the task pane.


export async function run() {
  // for Get a reference to the current message
  var item = Office.context.mailbox.item;
  // for Write message property value to the task pane
  document.getElementById(&quot;item-subject&quot;).innerHTML = &quot;<b>Subject:</b> 
&quot; + item.subject;
}

Try it out

Notice that Office Add-ins need to use HTTPS, not HTTP, also when you are developing. If you are prompted to install a certificate after you run the below-mentioned command, accept the prompt to install the certificate that the Yeoman generator offers. You can also have to run your command prompt or terminal as an administrator for change to be done.

Run the below-mentioned command in the root directory of your project. Whenever you run this command, the local web server will start if it is not running already.

npm run dev-server

After run dev-server the manifest.xml code looks like the following:


<?xml encoding="UTF-8" standalone="yes" version="1.0">
<officeapp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" xmlns:mailappor="http://schemas.microsoft.com/office/mailappversionoverrides/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MailApp">
  <id>8b211c93-f677-4d2c-a711-4d1db3d9eaff</id>
  <version>1.0.0.0</version>
  <providername>Contoso</providername>
  <defaultlocale>en-US</defaultlocale>
  <displayname defaultvalue="My OutlookDemo add-in" />
  <description defaultvalue="A template to get started." />
  <iconurl defaultvalue="https://localhost:3000/assets/icon-64.png" />
  <highresolutioniconurl defaultvalue="https://localhost:3000/assets/icon-128.png" />
  <supporturl defaultvalue="https://www.contoso.com/help" />
  <appdomains>
    <appdomain>https://www.contoso.com</appdomain>
  </appdomains>
  <hosts>
    <host name="Mailbox" />
  </hosts>
  <requirements>
    <sets>
      <set minversion="1.1" name="Mailbox" />
    </sets>
  </requirements>
  <formsettings><form xsi:type="ItemRead">
      <desktopsettings>
        <sourcelocation defaultvalue="https://localhost:3000/taskpane.html" />
        <requestedheight>250</requestedheight>
      </desktopsettings></form>
  </formsettings>
  <permissions>ReadWriteItem</permissions>
  <rule mode="Or" xsi:type="RuleCollection">
    <rule formtype="Read" itemtype="Message" xsi:type="ItemIs" />
  </rule>
  <disableentityhighlighting>false</disableentityhighlighting>
  <versionoverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0">
    <requirements>
      <bt:sets defaultminversion="1.3">
        <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 id="msgReadOpenPaneButton" xsi:type="Button">
                  <label resid="TaskpaneButton.Label">
                  <supertip><title resid="TaskpaneButton.Label"><description resid="TaskpaneButton.Tooltip" /></title></supertip>
                  <icon>
                    <bt:image resid="Icon.16x16" size="16" />
                    <bt:image resid="Icon.32x32" size="32" />
                    <bt:image resid="Icon.80x80" size="80" />
                  </icon>
                  <action xsi:type="ShowTaskpane">
                    <sourcelocation resid="Taskpane.Url" />
                  </action>
                </label></control><label resid="TaskpaneButton.Label">
                <control id="ActionButton" xsi:type="Button">
                  <label resid="ActionButton.Label">
                  <supertip><title resid="ActionButton.Label"><description resid="ActionButton.Tooltip" /></title></supertip>
                  <icon>
                    <bt:image resid="Icon.16x16" size="16" />
                    <bt:image resid="Icon.32x32" size="32" />
                    <bt:image resid="Icon.80x80" size="80" />
                  </icon>
                  <action xsi:type="ExecuteFunction">
                    <functionname>action</functionname>
                  </action>
                </label></control><label resid="ActionButton.Label">
              </label></label></label></group><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
            </label></label></label></officetab><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
          </label></label></label></extensionpoint><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
        </label></label></label></desktopformfactor><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
      </label></label></label></host><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
    </label></label></label></hosts><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
    <resources>
      <bt:images>
        <bt:image defaultvalue="https://localhost:3000/assets/icon-16.png" id="Icon.16x16" />
        <bt:image defaultvalue="https://localhost:3000/assets/icon-32.png" id="Icon.32x32" />
        <bt:image defaultvalue="https://localhost:3000/assets/icon-80.png" id="Icon.80x80" />
      </bt:images>
      <bt:urls>
        <bt:url defaultvalue="https://localhost:3000/commands.html" id="Commands.Url" />
        <bt:url defaultvalue="https://localhost:3000/taskpane.html" id="Taskpane.Url" />
      </bt:urls>
      <bt:shortstrings>
        <bt:string defaultvalue="Contoso Add-in" id="GroupLabel" />
        <bt:string defaultvalue="Show Taskpane" id="TaskpaneButton.Label" />
        <bt:string defaultvalue="Perform an action" id="ActionButton.Label" />
      </bt:shortstrings>
      <bt:longstrings>
        <bt:string defaultvalue="Opens a pane displaying all available properties." id="TaskpaneButton.Tooltip" />
        <bt:string defaultvalue="Perform an action when clicked." id="ActionButton.Tooltip" />
      </bt:longstrings>
    </resources>
  </label></label></label></versionoverrides><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">
</label></label></label></officeapp><label resid="GroupLabel"><label resid="TaskpaneButton.Label"><label resid="ActionButton.Label">

</label></label></label></?xml>