Share via


File and folder properties (Windows Runtime apps using JavaScript and HTML)

Your Windows Runtime apps using JavaScript and HTML can use the Live SDK to read user's Microsoft OneDrive folders and files.

In this article
Prerequisites
Read a file's properties
Update a file's properties
Read a folder's properties
Update a folder's properties
Get links to files and folders
Summary

Prerequisites

We assume that the user has already signed in and consented to the wl.skydrive scope for reading file or folder properties. If you have not added user sign-in to your Windows Runtime apps, see Signing users in.

Note

In this context, a file represents a file of any supported format, including a photo, video, or audio. A folder can contain child folders and files.

Read a file's properties

To get info about a file, photo, video, or audio, use code like this. The wl.skydrive scope is required.

function readFileProperties_onClick() {
    WL.login({ 
        scope: "wl.skydrive" 
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!152",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = 
                        "File properties: 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;
        }
    );
}

Update a file's properties

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

function updateFileProperties_onClick() {
    WL.login({ 
        scope: "wl.skydrive_update"
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!152",
                method: "PUT",
                body: {
                    name: "AnotherOneNoteNotebook"
                } 
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = 
                        "Updated file properties. Name: " + response.name + ", ID: " + response.id;
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                } 
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Login error.";
        }
    );
}

Read a folder's properties

To read a folder's properties, use code like this. In this example, the folder ID identifies the folder. The HTTP GET method is used to get the folder's properties. The anonymous methods following .then with the response and responseFailed parameters determine whether the call is successful. If the call is successful, the folder properties are displayed.

function readFolderProperties_onClick() {
    WL.login({
        scope: "wl.skydrive"
    }).then(
        function (response) {
            WL.api({
                path: "folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!164",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = 
                        "Folder properties: name = " + response.name + ", ID = " + response.id;                
                }, 
                function (responseFailed) {
                    document.getElementById("infoArea").innerText = 
                        "Error reading folder properties: " + responseFailed.error.message;
                }
            );
        }, 
        function (responseFailed) {
            document.getElementById("infoArea").innerText = 
                "Error signing in: " + responseFailed.error_description;
        }
    );
}

Update a folder's properties

To change info for an existing folder, use code like this. The wl.skydrive_update scope is required.

function updateFolderProperties_onClick() {
    WL.login({
        scope: "wl.skydrive_update" 
    }).then(
        function (response) {
            WL.api({
                path: "folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!164",
                method: "PUT",
                body: {
                    name: "A different folder name"
                } 
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = 
                        "Updated 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;
        }
    );
}

Your apps can get the following types of links to OneDrive folders and files.

  • An embedded link, which is an HTML code snippet that you can insert into a webpage to provide an interactive view of the corresponding file.

    Note

    Currently, embedded links are supported only for files owned by the user. To determine whether a given file can be embedded, check to see if the corresponding File object's is_embeddable structure is set to true. Also, some embedded links provide a rich embedding experience, while others provide only an icon and a file name. Rich embedding is supported for the following types of Microsoft PowerPoint, Microsoft Excel, and Microsoft Word files: .doc, .docx, .pdf, .odt, .pot, .potm, .potx, .pps, .ppsm, .ppsx, .ppt, .pptm, .pptx, .xlsb, .xlsm, and .xlsx.

  • A read-only link, which is a link to a read-only version of the folder or file.

  • A read-write link, which is a link to a read-write version of the folder or file.

Note

When your apps request a read-only or read-write link to a file, the URL to that file is the same for either request. When your apps request a read-only link for a file and then later request a read-write link for that same file, read-only permission for that link is revoked. Similarly, when your apps request a read-write link for a file and then later request a read-only link for that same file, read-write permission for that link is revoked.

To get a link to a folder or file, use code like this.

function getSharedLink_onClick() {
    WL.login({
        scope: "wl.skydrive"
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!152/shared_read_link",
                method: "GET"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = 
                        "Shared link = " + response.link;
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                } 
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}

In the preceding code, change the file ID to the folder ID or file ID that you want a link to. Also, change shared_read_link to one of these.

  • To embed for an embedded link. You get the embed HTML code snippet from the returned JavaScript Object Notation (JSON)-formatted object's embed_html structure value.

  • To shared_read_link for a read-only link. You get the read-only link from the returned JSON-formatted object's link structure value.

  • To shared_edit_link for a read-write link. You get the read-write link from the returned JSON-formatted object's link structure value.

When your app requests a file, photo, video, or audio, the returned object includes a preauthenticated URL as the value of the source attribute. Preauthenticated URLs allow access to a file without the need for additional permissions. Preauthenticated URLs expire and should not be stored or reused later. The following example shows what a preauthenticated URL looks like.

"source": "http://storage.live.com/s1pk0Hsk4twGIbzleNLcqhVhp5yvj9aPLPMHq2GDTRFBeuicbS27Fx5xBnmyy5J4lDSEtdvYm2FxOB0-s8Heb1l6hq3A1E2bJ9qWQ3GdIUTnnG6CHLTBLdCHRPUNAunekX1dnLS49vG-fUV7O8ZvrkiLTubXXFqrT1roxZ1oJjELE/Test.txt:Binary"

Summary

You have just learned how to read folder and file properties on OneDrive, from your Windows Runtime app using JavaScript and HTML. Next, you might want to allow users to upload or download files on to OneDrive. To learn how to upload or download files on OneDrive, see Downloading and uploading files.