Microsoft 365 Outlook for Business: Remove Category from Email

Microsoft 365 Outlook for Business: Remove Category from Email

Microsoft 365 Outlook for Business: メールのカテゴリ解除

This item removes categories from an email message in Outlook. Multiple categories can be removed at once.

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 Setting *
conf_MessageId
C2: Message ID *
conf_Category
C3: Categories to remove (write one per line) *

Notes

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • outlook-message-category-remove.xml (C) Questetra, Inc. (MIT License)
    • If you are using Professional, you can modify the contents of this file and use it as your own add-on auto step

// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/authorize
// - Token Endpoint URL: https://login.microsoftonline.com/{your-tenant-id}/oauth2/v2.0/token
// - Scope: https://graph.microsoft.com/Mail.ReadWrite offline_access
// - Consumer Key: (Get by Microsoft Azure Active Directory)
// - Consumer Secret: (Get by Microsoft Azure Active Directory)

function main(){
    //// == 工程コンフィグ・ワークフローデータの参照 / Config & Data Retrieving ==
    const auth = configs.getObject('conf_OAuth2');
    const messageId = retrieveMessageId();
    const categoriesToRemove = retrieveCategories();

    //// == 演算 / Calculating ==
    const currentCategories = getMessageCategories(auth, messageId);
    const newCategories = currentCategories.filter(category => !categoriesToRemove.includes(category));
    if (newCategories.length < currentCategories.length) { // 差分がある場合のみ、更新
        updateMessageCategories(auth, messageId, newCategories);
    }
}

/**
 * config からメール ID を読み出す
 * 空の場合はエラー
 * @returns {String} メール ID
 */
function retrieveMessageId() {
    const messageId = engine.findData(configs.getObject('conf_MessageId'));
    if (messageId === null) {
        throw 'Message ID is blank.';
    }
    return messageId;
}

/**
 * ワークフローデータからカテゴリの一覧を読み出す
 * 末尾の改行のみ無視し、他の空行はエラー
 * @returns {Array<String>} categories カテゴリ一覧
 */
function retrieveCategories() {
    let categoriesStr = configs.get('conf_Category');
    if (categoriesStr.endsWith('\n')) { // 末尾に空行がある場合、削除
        categoriesStr = categoriesStr.slice(0, -1);
    }
    const categories = categoriesStr.split('\n');
    if (categories.some(line => line === '')) {
        throw 'Empty lines are not allowed for category.';
    }
    return categories;
}

/**
 * Microsoft Graph API にメール取得の GET リクエストを送信し、カテゴリ一覧を取得する
 * @param {AuthSettingWrapper} auth 認証設定
 * @param {String} messageId メール ID
 * @returns {Array<String>} categories メールに設定されたカテゴリ一覧
 */
function getMessageCategories(auth, messageId) {
    const response = httpClient.begin()
        .authSetting(auth)
        .queryParam('$select', 'categories')
        .get(`https://graph.microsoft.com/v1.0/me/messages/${encodeURI(messageId)}`);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw `Failed to get message. status: ${status}`;
    }
    return JSON.parse(responseStr).categories;
}

/**
 * Microsoft Graph API にメール更新の PATCH リクエストを送信し、カテゴリ一覧を置き換える
 * @param {AuthSettingWrapper} auth 認証設定
 * @param {String} messageId メール ID
 * @param {Array<String>} categories カテゴリ一覧
 */
function updateMessageCategories(auth, messageId, categories) {
    const body = {categories};
    const response = httpClient.begin()
        .authSetting(auth)
        .body(JSON.stringify(body), 'application/json; charset=UTF-8')
        .patch(`https://graph.microsoft.com/v1.0/me/messages/${encodeURI(messageId)}`);
    const status = response.getStatusCode();
    if (status !== 200) {
        engine.log(response.getResponseAsString());
        throw `Failed to update message. status: ${status}`;
    }
}

    
Scroll to Top

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading