Microsoft Lists: リストアイテム削除

Microsoft Lists: リストアイテム削除

Microsoft Lists: Delete List Item

この工程は、Microsoft Lists のリストアイテムを削除します。一度に複数の削除が可能です。複数削除する場合、文字型データ項目では 1 行につき 1 つずつリストアイテム ID を書くようにしてください。

Basic Configs
工程名
メモ
Auto Step icon
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 設定 *
conf_SiteUrl
C2: SharePoint サイトの URL *
conf_ListTitle
C3: リストの名前 *
conf_ListItemIds
C4: 削除するリストアイテムの ID が保存されているデータ項目 *

Notes

  • SharePoint サイトの URL は、リスト URL の /Lists より前の部分です
    • マイリスト内のリストの場合、https://{サブドメイン}-my.sharepoint.com/personal/{ユーザ識別子}_{サブドメイン}_onmicrosoft_com のような形式です
    • SharePoint サイト内のリストの場合、https://{サブドメイン}.sharepoint.com/sites/{サイト識別子} のような形式です

Capture

See Also

Script (click to open)
  • 次のスクリプトが記述されている XML ファイルをダウンロードできます
    • microsoft-lists-listitem-delete.xml (C) Questetra, Inc. (MIT License)
    • Professional のワークフロー基盤では、ファイル内容を改変しオリジナルのアドオン自動工程として活用できます

// 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/Sites.ReadWrite.All offline_access
// - Consumer Key: (Get by Microsoft Azure Active Directory)
// - Consumer Secret: (Get by Microsoft Azure Active Directory)

const GRAPH_URI = 'https://graph.microsoft.com/v1.0/';

const main = () => {
    //// == 工程コンフィグの参照 / Config Retrieving ==
    const oauth2 = configs.getObject('conf_OAuth2');
    const siteUrl = retrieveSiteUrl();
    const listTitle = configs.get('conf_ListTitle');
    const listItemIds = retrieveListItemIds();

    //// == 演算 / Calculating ==
    const siteId = getSiteIdByUrl(oauth2, siteUrl);
    listItemIds.forEach(listItemId => {
        deleteListItem(oauth2, siteId, listTitle, listItemId);
    });
};

/**
 * 工程コンフィグからサイトの URL を取得する
 * @returns {String} サイトの URL
 */
const retrieveSiteUrl = () => {
    let siteUrl = configs.get('conf_SiteUrl');
    // 末尾にスラッシュがある場合、削除
    if (siteUrl.endsWith('/')) {
        siteUrl = siteUrl.slice(0, -1);
    }
    return siteUrl;
};

/**
 * 工程コンフィグからリストアイテム ID を取得する
 * @returns {Set<String>} リストアイテム ID
 */
const retrieveListItemIds = () => {
    const dataDef = configs.getObject('conf_ListItemIds');
    let ids = [];
    if (dataDef.matchDataType('SELECT')) { // 選択型データ項目の場合
        const select = engine.findData(dataDef);
        if (select === null || select.size() === 0) { // 未選択
            throw new Error('No list item IDs selected.');
        }
        for (let i = 0; i < select.size(); i++) {
            ids.push(select.get(i).getValue());
        }
    } else { // 文字型データ項目の場合
        const value = engine.findData(dataDef);
        if (value === null) {
            throw new Error('No list item IDs.');
        }
        ids = value.split('\n').filter(id => id !== '');
        if (ids.length === 0) {
            throw new Error('No list item IDs.');
        }
    }
    const idsSet = new Set(ids); // 重複を除外
    if (idsSet.size + 1 > httpClient.getRequestingLimit()) { // 1 additional request to get site ID
        throw new Error('Too many list item IDs.');
    }
    return idsSet;
};

/**
 * サイトのメタデータを取得し、siteId を返す
 * APIの仕様: https://docs.microsoft.com/ja-jp/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online
 * @param {AuthSettingWrapper} oauth2  OAuth2 設定情報
 * @param {String} siteUrl  SharePoint サイトの URL
 * @returns {String} siteId
 */
const getSiteIdByUrl = (oauth2, siteUrl) => {
    // encoding sharing URL
    const encodedSharingUrl = encodeSharingUrl(siteUrl);

    // preparing for API Request
    const response = httpClient.begin()
        .authSetting(oauth2)
        .queryParam('select', 'id')
        .get(`${GRAPH_URI}shares/${encodedSharingUrl}/site`);
    const status = response.getStatusCode();
    const responseStr = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseStr);
        throw new Error(`Failed to get site info. status: ${status}`);
    }
    return JSON.parse(responseStr).id;
};

/**
 * 共有URLを unpadded base64url 形式にエンコードする
 * @param {String} sharingUrl  共有URL
 * @returns {String} encodedSharingUrl  エンコードされた共有URL
 */
const encodeSharingUrl = (sharingUrl) => {
    let encodedSharingUrl = base64.encodeToUrlSafeString(sharingUrl);
    while (encodedSharingUrl.slice(-1) === '=') {
        encodedSharingUrl = encodedSharingUrl.slice(0, -1);
    }
    return `u!${encodedSharingUrl}`;
};

/**
 * リストアイテムを削除する
 * @param {AuthSettingWrapper} oauth2  OAuth2 設定情報
 * @param {String} siteId  SharePoint サイトの ID
 * @param {String} listTitle  リストの名前
 * @param {String} listItemId  リストアイテム ID
 */
const deleteListItem = (oauth2, siteId, listTitle, listItemId) => {
    const url = `${GRAPH_URI}sites/${encodeURIComponent(siteId)}/lists/${encodeURIComponent(listTitle)}/items/${encodeURIComponent(listItemId)}`;
    const response = httpClient.begin()
        .authSetting(oauth2)
        .delete(url);
    const status = response.getStatusCode();
    if (status !== 204) {
        engine.log(response.getResponseAsString());
        throw new Error(`Failed to delete list item. id: ${listItemId}, status: ${status}`);
    }
};

    
上部へスクロール

Questetra Supportをもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む