Share via


Outlook で予定を作成するときに場所を取得または設定する

Office JavaScript API には、ユーザーが作成している予定の場所を管理するためのプロパティとメソッドが用意されています。 現在、予定の場所を提供するプロパティは 2 つあります。

  • item.location: 場所を取得して設定できる基本的な API。
  • item.enhancedLocation: 場所の取得と設定を可能にし、場所の 種類の指定を含む拡張 API。 型は、 を使用してitem.location場所を設定する場合ですLocationType.Custom

次の表に、場所 API と、使用可能なモード (新規作成または読み取り) を示します。

API 適用可能な予定モード
item.location Attendee/Read
item.location.getAsync Organizer/Compose
item.location.setAsync Organizer/Compose
item.enhancedLocation.getAsync Organizer/Compose,
Attendee/Read
item.enhancedLocation.addAsync Organizer/Compose
item.enhancedLocation.removeAsync Organizer/Compose

アドインの作成にのみ使用できるメソッドを使用するには、アドイン XML マニフェストを構成して、オーガナイザー/新規作成モードでアドインをアクティブにします。 詳細については、「Create Outlook アドイン」を参照してください。 ライセンス認証規則は、 Microsoft 365 の統合マニフェストを使用するアドインではサポートされていません。

API を使用するenhancedLocation

API を enhancedLocation 使用して、予定の場所を取得および設定できます。 場所フィールドは複数の場所をサポートしており、場所ごとに表示名、種類、会議室のメール アドレス (該当する場合) を設定できます。 サポートされている場所の種類については、「 LocationType 」を参照してください。

場所の追加

次の例では、mailbox.item.enhancedLocationaddAsync を呼び出して場所を追加する方法を示します。

let item;
const locations = [
    {
        "id": "Contoso",
        "type": Office.MailboxEnums.LocationType.Custom
    }
];

Office.initialize = function () {
    item = Office.context.mailbox.item;
    // Check for the DOM to load using the jQuery ready method.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.
        // Add to the location of the item being composed.
        item.enhancedLocation.addAsync(locations);
    });
}

場所を取得する

次の例は、mailbox.item.enhancedLocationgetAsync を呼び出して場所を取得する方法を示しています。

let item;

Office.initialize = function () {
    item = Office.context.mailbox.item;
    // Checks for the DOM to load using the jQuery ready method.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.
        // Get the location of the item being composed.
        item.enhancedLocation.getAsync(callbackFunction);
    });
}

function callbackFunction(asyncResult) {
    asyncResult.value.forEach(function (place) {
        console.log("Display name: " + place.displayName);
        console.log("Type: " + place.locationIdentifier.type);
        if (place.locationIdentifier.type === Office.MailboxEnums.LocationType.Room) {
            console.log("Email address: " + place.emailAddress);
        }
    });
}

注:

予定の場所として追加された個人用連絡先グループは、enhancedLocation.getAsync メソッドによって返されません。

場所を削除する

次の例では、mailbox.item.enhancedLocationremoveAsync を呼び出して場所を削除する方法を示します。

let item;

Office.initialize = function () {
    item = Office.context.mailbox.item;
    // Checks for the DOM to load using the jQuery ready method.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.
        // Get the location of the item being composed.
        item.enhancedLocation.getAsync(callbackFunction);
    });
}

function callbackFunction(asyncResult) {
    asyncResult.value.forEach(function (currentValue) {
        // Remove each location from the item being composed.
        item.enhancedLocation.removeAsync([currentValue.locationIdentifier]);
    });
}

API を使用するlocation

API を location 使用して、予定の場所を取得および設定できます。

場所を取得する

ここでは、ユーザーが新規作成している予定の配置場所を取得し、それを表示するコード サンプルを示します。

を使用 item.location.getAsyncするには、非同期呼び出しの状態と結果をチェックするコールバック関数を指定します。 省略可能なパラメーターを使用して、コールバック関数に必要な引数を asyncContext 指定できます。 コールバックの出力パラメーター asyncResult を使用して、状態、結果、エラーを取得できます。 非同期コールが成功した場合、AsyncResult.value プロパティを使用して、配置場所を文字列として取得することができます。

let item;

Office.initialize = function () {
    item = Office.context.mailbox.item;
    // Checks for the DOM to load using the jQuery ready method.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.
        // Get the location of the item being composed.
        getLocation();
    });
}

// Get the location of the item that the user is composing.
function getLocation() {
    item.location.getAsync(
        function (asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed){
                write(asyncResult.error.message);
            }
            else {
                // Successfully got the location, display it.
                write ('The location is: ' + asyncResult.value);
            }
        });
}

// Write to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message;
}

場所を設定する

ここでは、ユーザーが新規作成している予定の配置場所を設定するコード サンプルを示します。

item.location.setAsync を使用するには、data パラメーターに最大 255 文字までの文字列を指定します。 必要に応じて、 パラメーターにコールバック関数とコールバック関数の任意の引数を asyncContext 指定できます。 コールバックの出力パラメーターに状態、結果、およびエラー メッセージをasyncResultチェックする必要があります。 非同期呼び出しが成功した場合、setAsync はそのアイテムの既存の配置場所を上書きし、指定した配置場所をプレーンテキストとして挿入します。

注:

区切り記号としてセミコロンを使用して複数の場所を設定できます (例: '会議室 A;会議室 B')。

let item;

Office.initialize = function () {
    item = Office.context.mailbox.item;
    // Check for the DOM to load using the jQuery ready method.
    $(document).ready(function () {
        // After the DOM is loaded, app-specific code can run.
        // Set the location of the item being composed.
        setLocation();
    });
}

// Set the location of the item that the user is composing.
function setLocation() {
    item.location.setAsync(
        'Conference room A',
        { asyncContext: { var1: 1, var2: 2 } },
        function (asyncResult) {
            if (asyncResult.status == Office.AsyncResultStatus.Failed){
                write(asyncResult.error.message);
            }
            else {
                // Successfully set the location.
                // Do whatever is appropriate for your scenario,
                // using the arguments var1 and var2 as applicable.
            }
        });
}

// Write to a div with id='message' on the page.
function write(message){
    document.getElementById('message').innerText += message;
}

関連項目