File and folder properties on OneDrive (Android)

Your Android apps can use the Live SDK to read a 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 Android apps, see Signing users in (Android).

We assume that you are familiar with Java, and can write Android apps.

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.

Note

In all examples, change folderID or fileID with a different folder or file ID, respectively.

public void readFile() {
    String fileId =  "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!141";
       client.getAsync(fileId, new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error reading file: " + exception.getMessage());
           }
           public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String text = "File info:" +
                "\nID = " + result.optString("id") +
                "\nName = " + result.optString("name");
               resultTextView.setText(text);
           }
       });
}

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.

public void updateFile() {
    final String fileId = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!141";
    final LiveOperationListener opListener = new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error updating file: " + exception.getMessage());
           }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String text = "File updated:" +
                "\nID = " + result.optString("id") +
                "\nName = " + result.optString("name");
               resultTextView.setText(text);
           }
       };
    auth.login(this, Arrays.asList(new String[] { "wl.skydrive_update" }), 
        new LiveAuthListener() {
               public void onAuthError(LiveAuthException exception, Object userState) {
                   resultTextView.setText("Error signing in: " + exception.getMessage());
               }
            public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
                try {
                    JSONObject body = new JSONObject();
                    body.put("name", "weather.txt");
                    body.put("description", "Local weather");
                       client.putAsync(fileId, body, opListener);    
                }
                catch(JSONException ex){
                    resultTextView.setText("Error signing in: " + ex.getMessage());
                }
               }
        }
    );     
}

Read a folder's properties

To read a folder's properties, use code like this. In this example, the getAsync method is used to get the folder's properties. The onComplete and onError methods determine whether the Live SDK APIs are successfully called, and if so, display some of the folder's properties.

public void readFolder() {
    client.getAsync("folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!125", new LiveOperationListener() {
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            resultTextView.setText("Folder ID = " + result.optString("id") + 
                ", name = " + result.optString("name"));
        }
        public void onError(LiveOperationException exception, LiveOperation operation) {
            resultTextView.setText("Error reading folder: " + exception.getMessage());
        }
    });
}

Update a folder's properties

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

public void updateFolder() {
    final String folderId = "folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!125";
    final LiveOperationListener opListener = new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error updating folder: " + exception.getMessage());
           }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String text = "Folder updated:\n" +
                "\nID = " + result.optString("id") +
                "\nName = " + result.optString("name");
               resultTextView.setText(text);
           }
       };
    auth.login(this, Arrays.asList(new String[] { "wl.skydrive_update" }), 
        new LiveAuthListener() {
               public void onAuthError(LiveAuthException exception, Object userState) {
                   resultTextView.setText("Error signing in: " + exception.getMessage());
               }
            public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
                try {
                    JSONObject body = new JSONObject();
                    body.put("name", "MyFolderIsUpdated");
                    body.put("description", "My folder is updated");
                    client.putAsync(folderId, body, opListener);    
                }
                catch(JSONException ex) {
                    resultTextView.setText("Error updating folder: " + ex.getMessage());
                }
            }
        }
    );  
}

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 web page 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.

public void getLinkToFile() {
    final String path = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!141/shared_read_link";
    final LiveOperationListener opListener = new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error getting link to file: " + exception.getMessage());
           }
           public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String text =  "Shared link to file = " + result.optString("link");
               resultTextView.setText(text);
           }
       };
    auth.login(this, Arrays.asList(new String[] { "wl.skydrive" }), new LiveAuthListener() {
        public void onAuthError(LiveAuthException exception, Object userState) {
            resultTextView.setText("Error signing in: " + exception.getMessage());
        }
        public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState) {
            client.getAsync(path, opListener);
        }
    });        
}

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 add the ability read folder and file properties on OneDrive from your Android apps. Next, you might want to allow users to upload or download files on OneDrive. To learn how to upload/download files on OneDrive, see Downloading and uploading files on OneDrive (Android).