Box: Move Folder

Box: Move Folder

Box: フォルダ移動

This item moves a folder to the specified folder. The Folder ID and URL won’t change.

Basic Configs
Step Name
Note
Auto Step icon
Configs for this Auto Step
conf_OAuth2
C1: OAuth2 Setting *
conf_FolderId
C2: Folder ID to move *
conf_ParentFolderId
C3: Destination Folder ID *

Notes

  • Folder ID is contained in the URL: https://{sub-domain}.app.box.com/folder/(Folder ID)
  • If there are folder name conflicts it will result in an error
  • Box refresh tokens have an expiration date

Capture

See Also

Script (click to open)
  • An XML file that contains the code below is available to download
    • box-folder-move.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


const main = () => {
    //// == Config Retrieving / 工程コンフィグの参照 ==
    const oauth2 = configs.getObject("conf_OAuth2");
    const folderId = decideFolderId();
    const parentFolderId = decideParentFolderId();
    if (folderId === parentFolderId) {
        throw new Error('Folder cannot be moved to itself.');
    }

    //// == Calculating / 演算 ==
    moveFolder(oauth2, folderId, parentFolderId);
};

/**
  * 移動させるフォルダの ID を config から読み出す
  * 空の場合はエラー
  * @return {String}  移動させるフォルダの ID
  */
const decideFolderId = () => {
    const folderIdDef = configs.getObject("conf_FolderId");
    const folderId = engine.findData(folderIdDef);
    if (folderId === null) {
        throw new Error("Folder ID to move is blank.");
    }
    if (folderId === '0') {
        throw new Error('Root folder cannot be moved.');
    }
    return folderId;
};

/**
  * 移動先フォルダの ID を config から読み出す
  * 空の場合はエラー
  * @return {String}  移動先フォルダの ID
  */
const decideParentFolderId = () => {
    let folderId = "";
    const folderIdDef = configs.getObject("conf_ParentFolderId");
    if (folderIdDef === null) {
        folderId = configs.get("conf_ParentFolderId");
    } else {
        folderId = engine.findData(folderIdDef);
    }
    if (folderId === "" || folderId === null) {
        throw new Error("Destination Folder ID is blank.");
    }
    return folderId;
};

/**
  * Box 上のファイルを指定フォルダに移動する。
  * @param {Object} oauth2
  * @param {String} folderId
  * @param {String} parentFolderId
  */
const moveFolder = (oauth2, folderId, parentFolderId) => {
    const url = `https://api.box.com/2.0/folders/${encodeURIComponent(folderId)}`;
    const jsonBody = {
        "parent": {
            "id": parentFolderId
        }
    };

    const response = httpClient.begin()
        .authSetting(oauth2)
        // fields には id を指定(レスポンスを基本レプリゼンテーションに縮小するため)
        .queryParam("fields", "id")
        .body(JSON.stringify(jsonBody), "application/json; charset=UTF-8")
        .put(url);
    const status = response.getStatusCode();
    const responseTxt = response.getResponseAsString();
    if (status !== 200) {
        engine.log(responseTxt);
        throw new Error(`Failed to move. status: ${status}`);
    }
};

Scroll to Top

Discover more from Questetra Support

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

Continue reading