
Box: Delete Collaboration
This item deletes a collaboration with the specified user from the specified file or folder on Box. The user is specified by the email address. If all the information does not match, it cannot be deleted.
Configs: Common
- Step Name
- Note
Configs
- C1: OAuth2 Setting *
- C2: Email address of the Box user to delete a collaboration with *
- C3: Role *
- C4: Item type (file or folder) *
- C5: File/Folder ID to delete a collaboration from *
Notes
- Collaboration cannot be deleted unless the Email, Role, and Item type all match
- The refresh token for Box has an expiration date. Use regularly to ensure that it does not exceed the expiration
Capture

See also
Script (click to open)
- An XML file that contains the code below is available to download
- box-collaboration-delete.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
main();
function main(){
const oauth2 = configs.get("conf_OAuth2");
const itemType = configs.get("conf_ItemType");
const fileOrFolderId = decideFileOrFolderId(itemType);
const emailAddress = decideEmailAddress();
const role = configs.get("conf_Role");
// コラボレーションのリストを取得
const collaborationList = getCollaborationList(oauth2, itemType, fileOrFolderId);
// コラボレーションIDを探す
const collaborationId = findCollaborationId(collaborationList, emailAddress, role);
// コラボレーション削除
deleteCollaboration(oauth2, collaborationId);
}
/**
* 指定するメールアドレスをconfigから読み出して出力する
* @return {String} emailAddress メールアドレス
*/
function decideEmailAddress(){
let emailAddress = "";
const emailAddressDef = configs.getObject("conf_EmailAddress");
if(emailAddressDef === null){
emailAddress = configs.get("conf_EmailAddress");
}else{
emailAddress = engine.findData(emailAddressDef);
}
if(emailAddress === "" || emailAddress === null) {
throw "Email address is blank";
}
return emailAddress;
}
/**
* コラボレーションを削除するファイルもしくはフォルダのIDをconfigから読み出して出力する
* @param {String} itemType アクセス権限が付与される項目の種類 ファイル/フォルダ
* @return {String} id ファイル もしくは フォルダの ID
*/
function decideFileOrFolderId(itemType){
let id = "";
const idDef = configs.getObject("conf_ItemId");
if(idDef === null){
id = configs.get("conf_ItemId");
}else{
id = engine.findData(idDef);
}
if(id === "" || id === null) {
throw "File ID or Folder ID is blank";
}
if(itemType === "folders" && id === "0") {
throw "Collaborations not available on root folder";
}
return id;
}
/**
* ファイルまたはフォルダのコラボレーションのリストを取得する
* @param {String} oauth OAuth2 設定
* @param {String} itemType 項目の種類 ファイル/フォルダ
* @param {String} fileOrFolderId ファイル/フォルダの ID
* @return {Object} collaborationList コラボレーションのリストを格納した JSON オブジェクト
*/
function getCollaborationList(oauth2, itemType, fileOrFolderId) {
const url = `https://api.box.com/2.0/${itemType}/${fileOrFolderId}/collaborations/`;
const response = httpClient.begin()
.authSetting(oauth2)
.queryParam("fields", "id,accessible_by,invite_email,role")
.get(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status !== 200) {
engine.log(responseTxt);
throw `Failed to get information. status: ${status}`;
}
const jsonRes = JSON.parse(responseTxt);
if (jsonRes.entries.length === 0) {
throw `No collaborations in ID: ${fileOrFolderId}`;
}
return jsonRes.entries;
}
/**
* コラボレーションのリストからコラボレーション ID を探す
* @param {Object} collaborationList コラボレーションのリストを格納した JSON オブジェクト
* @param {String} emailAddress メールアドレス
* @param {String} role ロール
* @return {String} collaborationId コラボレーション ID
*/
function findCollaborationId(collaborationList, emailAddress, role) {
for (let i = 0; i < collaborationList.length; i++){
if(collaborationList[i].role !== role){
continue;
}
if(collaborationList[i].accessible_by === null){
// 未承認ユーザの場合、招待メールアドレスをチェック
if(collaborationList[i].invite_email === emailAddress){
return collaborationList[i].id;
}
continue;
}
if(collaborationList[i].accessible_by.login === emailAddress){
return collaborationList[i].id;
}
}
throw `Could not find the collaboration`;
}
/**
* コラボレーションを削除する
* @param {String} oauth2 Oauth2設定
* @param {String} collaborationId コラボレーション ID
*/
function deleteCollaboration(oauth2, collaborationId) {
const url = `https://api.box.com/2.0/collaborations/${collaborationId}`;
const response = httpClient.begin()
.authSetting(oauth2)
.delete(url);
const status = response.getStatusCode();
const responseTxt = response.getResponseAsString();
if (status !== 204) {
engine.log(responseTxt);
throw `Failed to delete the collaboration. status: ${status}`;
}
}