Search code examples
javascriptoffice-jsoffice-addinsoutlook-web-addins

Office Outlook Add-In JavaScript API


I am developing an Outlook Office-Add-In for Email Specification, I am having a challenge to call the Send Email Button to restrict users from submitting the email before they could have specified the email, and I am also having a challenge to pop out the Email Specification pane, please assist by showing me how do I call the send button to push users to classify the email before sending

<!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. -->

<!DOCTYPE html>

        <!-- Office JavaScript API -->
        
    
    Email Classification Prompt
    
    
        /* styles.css */
    
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
        }
    
        .container {
            width: 400px;
            margin: 50px auto;
            padding: 20px;
            background-color: #fff;
            border-radius: 10px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }
    
        h2 {
            font-size: 24px;
            margin-bottom: 20px;
        }
    
        .input-group {
            margin-bottom: 20px;
        }
    
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
    
        select {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 14px;
        }
    
        button {
            display: block;
            width: 100%;
            padding: 10px;
            background-color: #0078d4;
            color: white;
            border: none;
            border-radius: 5px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>
    
    
    
    
        <!-- Other email input fields (to, subject, body, etc.) go here -->
        
            <h2>NMI Email Classification</h2>
            Select Classification:
            
                Confidential
                Internal
                Public
            
            Classify Email
        
    
    
    
    Office.onReady((info) =\> {
        if (info.host === Office.HostType.Outlook) {
            document.getElementById("ClassifyButton").onclick = ClassifyEmail;
        }
    });
    
    
    
    function ClassifyEmail() {
        var classification = document.getElementById("classification").value;
        if (!classification) {
            alert("Please select a classification before sending.");
        } else {
            Office.context.mailbox.item.subject.setAsync(
                "\[" + classification + "\]",
                function (asyncResult) {
                    if (asyncResult.status == Office.AsyncResultStatus.Failed) {
                        console.error(asyncResult.error.message);
                    }
                }
            );
        }
    }

Solution

  • You have to configure your Outlook add-in for event-based tasks in your manifest file.

    Your function will trigger when user clicks on the Send button

    You have to read Office.context and validate the user's activity

    Here are all the references

    Configure event-based add-in

    SmartAlert OnMessageSend