File and folder properties (Windows Runtime apps using C#/VB and XAML)

Your Windows Runtime apps using C#/VB and XAML 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 Windows Runtime apps, see Signing users in (XAML).

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.

private async void btnReadFile_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.GetAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129");
        dynamic result = operationResult.Result;
        this.infoTextBlock.Text = string.Join(" ", "File name:", result.name, "ID:", result.id);
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting file info: " + exception.Message;
    }
}

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.

private async void btnUpdateFile_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var fileData = new Dictionary<string, object>();
        fileData.Add("name", "Tulips20.jpg");
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.PutAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129", fileData);
        this.infoTextBlock.Text = "File renamed.";
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error renaming file: " + exception.Message;
    }
}

Read a folder's properties

To read a folder's properties by using C#, use code like this. In this example, the GetAsync method gets the folder's properties. The GetAsync method determines whether the call was successful, and if so, displays some of the folder's properties.

private async void btnReadFolder_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.GetAsync("folder.8c8ce076ca27823f.8C8CE076CA27823F!126");
        dynamic result = operationResult.Result;
        infoTextBlock.Text = "Folder name: " + result.name + ", ID: " + result.id;
    }
    catch (LiveConnectException exception)
    {
        infoTextBlock.Text = "Error getting folder info: " + exception.Message;
    }
}

Update a folder's properties

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

private async void btnUpdateFolder_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var folderData = new Dictionary<string, object>();
        folderData.Add("name", "Wildlife Pictures 5");
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.PutAsync("folder.8c8ce076ca27823f.8C8CE076CA27823F!126", folderData);
        dynamic result = operationResult.Result;
        this.infoTextBlock.Text = "Folder updated: " + result.name;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error updating folder: " + exception.Message;
    }
}

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.

private async void btnGetLink_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.GetAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129/shared_read_link");
        dynamic result = operationResult.Result;
        this.infoTextBlock.Text = "Shared read link: " + result.link;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting shared read link: " + exception.Message;
    }
}

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 Windows Runtime 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 (XAML).