Share via


Move, copy, create, or delete a file or folder (Windows Runtime apps using JavaScript and HTML)

You can move, copy, create, or delete a user's files or folders on Microsoft OneDrive, from your Windows Runtime apps using JavaScript and HTML.

In this article
Prerequisites
Move or copy a file or folder
Delete a file
Create a folder
Delete a folder
Remarks

Prerequisites

The user must be signed in with a Microsoft account. To learn how to sign users in from your app, see Signing users in (HTML).

Move or copy a file or folder

In this example, the file ID and folder ID identify the file to be moved and the destination folder. The HTTP MOVE method is used to move the file. The anonymous methods following .then with the response and responseFailed parameters determine whether the call was successful, and if so, display a confirmation message. To copy a file, replace MOVE with COPY. To move a folder, replace file ID with the folder ID of the folder you want to move.

Note

For move operations, the source can be anything except the root, and the destination must be the folder to which the item will be moved. Folders themselves cannot be copied. Also, OneDrive storage is structured so that move and copy operations cannot occur between the OneDrive folder hierarchies of different users. For example, even if user A can read user B's files, user A cannot copy or move them to his or her own OneDrive folders.

function moveFile_onClick() {
    WL.login({
        scope: "wl.skydrive_update"
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!141",
                method: "MOVE",
                body: {
                    destination: "folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!125"
                }
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = "Item moved.";
                }, 
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Login error: " + responseFailed.error_description;
        }
    );
}

Delete a file

To remove a file, photo, video, or audio, use code like this. The wl.skydrive_update scope is required.

Change the file ID to a different file ID, photo ID, video ID, or audio ID to remove a different file, photo, video, or audio.

function deleteFile_onClick() {
    WL.login({ 
        scope: "wl.skydrive_update"
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!152",
                method: "DELETE"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = "Deleted file.";
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                } 
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}

Create a folder

To add a new folder, use code like this. The wl.skydrive_update scope is required.

Change me/skydrive to the folder ID (or, in certain cases, the friendly name, like "My Pictures") of the folder in which the new folder will be created.

function createFolder_onClick() {
    WL.login({
        scope: "wl.skydrive_update"
    }).then(
        function (response) {
            WL.api({
                path: "me/skydrive",
                method: "POST",
                body: {
                    "name": "This is a new folder",
                    "description": "A new folder"
                } 
            }).then(
                function(response) {
                    document.getElementById("infoArea").innerText = 
                        "Created folder. Name: " + response.name + ", ID: " + response.id;
                }, 
                function (responseFailed) {
                    document.getElementById("infoArea").innerText = 
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText = 
                "Error signing in: " + responseFailed.error_description;
        }
    );
}

Delete a folder

To remove a folder, use code like this. The wl.skydrive_update scope is required.

Change the folder ID to the folder ID (or, in certain cases, the friendly name) of the folder that you want to remove.

function deleteFolder_onClick() {
    WL.login({
        scope: "wl.skydrive_update" 
    }).then(
        function (response) {
            WL.api({
                path: "folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!164",
                method: "DELETE"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = "Deleted folder";
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                } 
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}

Remarks

You can't create a file in OneDrive. You must create a file, and then upload the file to OneDrive.