Read, create, or delete tags (Windows Runtime apps using C#/VB and XAML)

Your Windows Runtime apps using C#/VB and XAML can read, create, and delete tags (a type of comment) that users can add to photos, videos, and audio.

In this article
Prerequisites
Read a tag
Create a tag
Delete a tag
Remarks

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).

Read a tag

To get info about tags, use the following code. The wl.skydrive scope is required. You can change the photo ID to a different photo ID, video ID, or audio ID, to get info about the tags for another photo, video, or audio. Specify a tag's ID to get info about that individual tag.

You can also:

  • Get a limited number of tags by using the limit parameter in the example code to specify the number of items to get. For example, to get the first two tags, use PHOTO_ID/tags?limit=2.

  • Set the first tag to start getting by using the offset parameter in the preceding code to specify the index of the first item to get. For example, to get two items starting at the third item, use PHOTO_ID/tags?limit=2&offset=3.

    Note

    In the JavaScript Object Notation (JSON)-formatted object that's returned, you can look in the paging object for the previous and next structures to get the offset and limit parameter values of the previous and next entries.

private async void btnReadTag_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.GetAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129/tags");
        StringBuilder tags = new StringBuilder("People tagged:\n");
        dynamic result = operationResult.Result;
        dynamic data = result.data;
        if (data.Count > 0)
        {
            foreach (dynamic userData in data)
            {
                dynamic user = userData.user;
                if (user != null)
                {
                    if (user.name != null)
                    {
                        if (tags.Length != 0)
                        {
                            tags.Append("\n");
                        }
                        tags.Append("\t");
                        tags.Append(user.name);
                        tags.Append("\n");
                        tags += "\t" + user["name"].ToString() + "\n";
                    }
                }
            }
            this.infoTextBlock.Text = tags.ToString();
        }
        else
        {
            this.infoTextBlock.Text = "No tags.";
        }
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting tag(s) info: " + exception.Message;
    }
}

Create a tag

To create a tag, use code like the following. Change the photo ID to a different photo ID, video ID, or audio ID, to add a tag to another photo or video. The wl.skydrive_update scope is required. You can create tags for items that are owned by, or shared with, the signed-in user.

  • To create a tag for an item that is owned by the signed-in user, replace the photo ID with the item's ID in the code that follows.

  • To create a tag for an item that is shared with the signed-in user, first use the wl.contacts_skydrive scope to make a GET request to /USER_ID/skydrive/shared, where USER_ID is either me or the user ID of the consenting user. Then, if the item is included in the response, replace the photo ID with the item's ID in the code that follows.

private async void btnCreateTag_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var userData = new Dictionary<string, object>();
        userData.Add("name", "Roberto Tamburello");
        userData.Add("id", "8c8ce076ca27823f");
        var tagData = new Dictionary<string, object>();
        tagData.Add("user", userData);
        tagData.Add("x", 43.8986);
        tagData.Add("y", 54.4138);
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.PostAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129/tags", tagData);
        dynamic result = operationResult.Result;
        this.infoTextBlock.Text = string.Join(" ", "Tag created, ID:", result.id);
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error creating tag: " + exception.Message;
    }
}

Delete a tag

To delete a tag, use code like the this. Change the tag ID to the tag ID of the tag that you want to remove. The wl.skydrive_update scope is required.

Note

Only the owner of an item can delete that item's tags.

private async void btnDeleteTag_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.DeleteAsync("tag.22688711f5410e6c.22688711f5410e6c!767.PRaXZrdHI1uYGQYi9CU0StrzHak");
        this.infoTextBlock.Text = "Tag deleted.";
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error deleting tag: " + exception.Message;
    }
}

Remarks

For details about the required and optional structures that your app must provide to use POST, see Tag object.