Microsoft 365 OneDrive for Business: ファイル / フォルダ削除

Microsoft 365 OneDrive for Business: ファイル / フォルダ削除

Microsoft 365 OneDrive for Business: Delete File / Folder

この工程は、OneDrive 上のファイル / フォルダを削除します。一度に複数の削除が可能です。1行につき1つのURLが含まれている必要があります。

Auto Step icon
Basic Configs
工程名
メモ
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 設定 *
conf_urls
C2: 削除するファイル / フォルダの URL が保存されている文字型データ項目 *

Notes

  • Microsoft 365 の OneDrive for Business で使用できる自動工程です。個人用の OneDrive では使用できません。
  • ファイルやフォルダの URL は、OneDrive でファイルやフォルダの詳細ウィンドウ(右上のiのアイコン)から「その他の詳細」へ進み、「パス」の隣のアイコンから取得します。(上部メニューの「共有」や「リンクのコピー」から取得した URL も使用できます)
    ファイル、フォルダURLの取得方法

Capture

See also

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

// OAuth2 config sample at [OAuth 2.0 Setting]
// - Authorization Endpoint URL: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
// - Token Endpoint URL: https://login.microsoftonline.com/common/oauth2/v2.0/token
// - Scope: https://graph.microsoft.com/Files.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/";

function main(){
  //// == 工程コンフィグの参照 / Config Retrieving ==
  const oauth2 = configs.getObject("conf_OAuth2");
  const urlsDef = configs.getObject("conf_urls");

  //// == ワークフローデータの参照 / Data Retrieving ==
  const urlArray = retrieveUrlArray(urlsDef);

  //// == 演算 / Calculating ==
  checkHttpRequestingLimit(urlArray);
  deleteItems( urlArray, oauth2 );
}

/**
  * ワークフローデータから URL の配列を読み出す
  * @param {ProcessDataDefinitionView} urlsDef  データ項目の ProcessDataDefinitionView
  * @return {Array<String>} urlArray  URL の配列
  */
function retrieveUrlArray( urlsDef ) {
  const urls = engine.findData(urlsDef);
  if (urls === "" || urls === null) {
    throw "File / Folder URLs aren't set.";
  }
  let urlArray = urls.split("\n");
  urlArray = urlArray.filter(url => url !== ""); // 空文字列を削除
  if (urlArray.length === 0) {
    throw "File / Folder URLs aren't set.";
  }
  return urlArray;
}

/**
  * HTTPリクエストの上限を超えないか確認する
  * @param {Array<String>} urlArray  URL の配列
  */
function checkHttpRequestingLimit( urlArray ) {
  if ( urlArray.length * 2 > httpClient.getRequestingLimit() ){
    throw "Necessary HTTP requests exceeds the limit.";
  }
}

/**
  * ドライブアイテム(ファイル/フォルダ)を削除する。
  * @param {Array<String>} urlArray  ドライブアイテムの URL の配列
  * @param {AuthSettingWrapper} oauth2  OAuth2 認証設定
  */
function deleteItems( urlArray, oauth2 ) {
  const itemNum = urlArray.length;
  for (let i = 0; i < itemNum; i++){
    const itemInfo = getItemInfoByUrl(urlArray[i],oauth2);
    deleteItem(itemInfo,oauth2,urlArray[i]);
  }
}

/**
  * ドライブアイテム(ファイル/フォルダ)の URL からアイテム情報(ドライブIDとアイテムID)を取得し、
  * オブジェクトで返す
  * @param {String} driveItemUrl  ドライブアイテムの URL
  * @param {AuthSettingWrapper} oauth2  OAuth2 認証設定
  * @return {Object} itemInfo  ドライブアイテム情報 {driveId, id}
  */
function getItemInfoByUrl( driveItemUrl, oauth2 ) {
  // 分割代入
  const {
    id,
    parentReference: {
      driveId
    }
  } = getObjBySharingUrl( driveItemUrl, oauth2 ); // driveItemUrlが不正ならここでエラー
  const itemInfo = {driveId, id};
  return itemInfo;
}

/**
  * OneDriveのドライブアイテム(ファイル/フォルダ)のメタデータを取得し、JSONオブジェクトを返す
  * APIの仕様:https://docs.microsoft.com/ja-jp/onedrive/developer/rest-api/api/shares_get?view=odsp-graph-online
  * @param {String} sharingUrl  ファイルの共有URL
  * @param {AuthSettingWrapper} oauth2  OAuth2 認証設定
  * @return {Object} responseObj  ドライブアイテムのメタデータのJSONオブジェクト
  */
function getObjBySharingUrl( sharingUrl, oauth2 ) {
  if (sharingUrl === "" || sharingUrl === null) {
    throw `Sharing URL is empty.`;
  }

  // encoding sharing URL
  const encodedSharingUrl = encodeSharingUrl(sharingUrl);

  // API Request
  const response = httpClient.begin() // HttpRequestWrapper
    .authSetting( oauth2 ) // Request HEADER (OAuth2 Token)
    .get( `${GRAPH_URI}shares/${encodedSharingUrl}/driveItem` ); // HttpResponseWrapper
  const httpStatus = response.getStatusCode();
  const responseStr = response.getResponseAsString();
  if (httpStatus >= 300) {
    const accessLog = `Deleting item: ${sharingUrl}\n---GET request--- ${httpStatus}\n${responseStr}\n`;
    engine.log(accessLog);
    throw `Failed to get drive item. status: ${httpStatus}`;
  }
  return JSON.parse( responseStr );
}

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

/**
  * ドライブアイテム(ファイル/フォルダ)を削除する。
  * @param {String,String} driveId,id  ドライブアイテムのドライブID、アイテムID
  * @param {AuthSettingWrapper} oauth2  OAuth2 認証設定
  * @param {String} driveItemUrl  ドライブアイテムの URL
  */
function deleteItem( {
  driveId,
  id
}, oauth2, driveItemUrl){
  const apiUrl = `${GRAPH_URI}drives/${driveId}/items/${id}`;
  const response = httpClient.begin()
    .authSetting( oauth2 )
    .delete(apiUrl);

  const responseJson = response.getResponseAsString();
  const status = response.getStatusCode();
  if (status >= 300) {
    //when error thrown
    const accessLog = `Deleting item: ${driveItemUrl}\n---DELETE request--- ${status}\n${responseJson}\n`;
    engine.log(accessLog);
    throw `Failed to delete. status: ${status}`;
  }
}
  

Questetra Supportをもっと見る

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

続きを読む

上部へスクロール