Read, create, or delete tags on OneDrive (iOS)

Your iOS apps can read, create, and delete tags (a type of comment) that users can add to photos, videos, and audio that are stored on OneDrive.

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

Microsoft OneDrive allows users to place a tag (comment) on their files stored in OneDrive. This topic will show you how to provide your iOS app users the ability to create, read, and delete tags that are attached to their files on OneDrive.

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 iOS apps, see Signing users in (iOS).

  • You are familiar with Objective-C, and can write iOS apps.

  • You have an iOS app to which you want to add OneDrive to.

Read a tag

To get info about tags, use code like this. 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, replace file.95d463b9efcca0e1.95D463B9EFCCA0E1!116/tags with PHOTO_ID/tags?limit=2 in the example below.

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

-(void) getPhotoTags
{
    [self.liveClient getWithPath:@"file.95d463b9efcca0e1.95D463B9EFCCA0E1!116/tags"
                        delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSArray *tags = [operation.result objectForKey:@"data"];
    for (NSDictionary *tag in tags)
    {
        NSDictionary *userInfo = [tag objectForKey:@"user"];
        NSString *positionX = [tag objectForKey:@"x"];
        NSString *positionY = [tag objectForKey:@"y"];
        NSString *tagId = [tag objectForKey:@"id"];
        NSString *userId = [userInfo objectForKey:@"id"];
        NSString *userName = [userInfo objectForKey:@"name"];
        self.displayLabel.text = [NSString stringWithFormat:
                                  @"Tag Properties:\n"
                                  @"\tId. %@\n"
                                  @"\tUser Id. %@\n"
                                  @"\tUser Name. %@\n"
                                  @"\tPosition X. %@\n"
                                  @"\tPosition Y. %@\n"
                                  , tagId,
                                  userId, 
                                  userName, 
                                  positionX,
                                  positionY];
    }   
}

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.

-(void) createTag
{
    NSDictionary * user = [[NSDictionary dictionaryWithObjectsAndKeys:
                            @"John Smith", @"name",
                            nil] retain];
    NSDictionary * newTag = [[NSDictionary dictionaryWithObjectsAndKeys:
                              @"50",@"x",
                              @"70",@"y",
                              user,@"user",
                              nil] retain];
    [self.liveConnectClient postWithPath:@"file.1cfa02017372544c.1CFA02017372544C!209/tags"
                                dictBody:newTag                                    
                                delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSDictionary *userInfo = [operation.result objectForKey:@"user"];
    NSString *positionX = [operation.result objectForKey:@"x"];
    NSString *positionY = [operation.result objectForKey:@"y"];
    NSString *tagId = [operation.result objectForKey:@"id"];
    NSString *userId = [userInfo objectForKey:@"id"];
    NSString *userName = [userInfo objectForKey:@"name"];
    self.displayLabel.text = [NSString stringWithFormat:
                              @"Tag Properties:\n"
                              @"\tId. %@\n"
                              @"\tUser Id. %@\n"
                              @"\tUser Name. %@\n"
                              @"\tPosition X. %@\n"
                              @"\tPosition Y. %@\n"
                              , tagId,
                              userId, 
                              userName, 
                              positionX,
                              positionY];
}

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.

-(void) deleteTags
{
    [self.liveClient deleteWithPath:@"tag.95d463b9efcca0e1.95d463b9efcca0e1!116.0c6Cdgik364WTnvMuzvSGOqadrc"
                           delegate:self];    
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"API call successful!" 
                                                    message:@"Tag deleted successfully"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil, 
                          nil];
    [alert show];
    [alert release];
}

Remarks

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