Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Working with Microsoft OneDrive comments

Your apps can use the Live Connect APIs to read, delete, and create comments that Microsoft OneDrive users can add to folders, files, albums, photos, and videos.

To make full use of the code examples in this topic, you can use them in the context of larger code reference samples that we provide in Working with the code examples. To request the required scopes, see the code patterns in Obtaining user consent.

Reading comments

To get info about comments, use code like this. The wl.photos or wl.skydrive scopes are required.

[JavaScript and HTML for Windows Store apps]


function enumerateItemComments_onClick() {
    WL.login({
        scope: "wl.skydrive"
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/comments",
                method: "GET"
            }).then(
                function (response) {
                    msg = "Comments: ";
                    items = response.data;
                        for (index in items) {
                            msg += "\n" + items[index].message + 
                                " (" + items[index].from.name + ")"
                        }
                    document.getElementById("infoArea").innerText = msg;
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                } 
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}


[C# for Windows Store apps and Windows Phone apps]


private async void btnReadComment_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.GetAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129/comments");
        dynamic result = operationResult.Result;
        dynamic data = result.data;
        StringBuilder comments = new StringBuilder("Comments:\n");
        if (data.Count > 0)
        {
            foreach (dynamic datum in data)
            {
                if (comments.Length != 0)
                {
                    comments.Append("\n");
                }

                comments.Append("\t");
                comments.Append(datum.message);
                comments.Append("\n");
            }

            this.infoTextBlock.Text = comments.ToString();
        }
        else
        {
            this.infoTextBlock.Text = "No comments.";
        }
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error getting comment(s) info: " + exception.Message;
    }
}


[Objective-C for iOS]


-(void) readComment
{
    [self.liveConnectClient getWithPath:@"comment.1cfa02017372544c.1CFA02017372544C!209.1CFA02017372544C!222"
                               delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSDictionary *from = [operation.result objectForKey:@"from"];
    NSString *commentId = [operation.result objectForKey:@"id"];
    NSString *commentMessage = [operation.result objectForKey:@"message"];
    NSString *commentDate = [operation.result objectForKey:@"created_time"];
    NSString *userId = [from objectForKey:@"id"];
    NSString *userName = [from objectForKey:@"name"];
    self.displayLabel.text = [NSString stringWithFormat:
                              @"Comment Properties:\n"
                              @"\tId. %@\n"
                              @"\tUser Id. %@\n"
                              @"\tUser Name. %@\n"
                              @"\tMessage. %@\n"
                              @"\tCreated Time. %@\n"
                              , commentId,
                              userId, 
                              userName, 
                              commentMessage,
                              commentDate];
}


[Java for Android]


public void readComments() {
    final String commentspath = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/comments";
    client.getAsync(commentspath , new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error reading comments: " + exception.getMessage());
           }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String msg = "Comment info:";
            JSONArray  items = result.optJSONArray("data");
            for (int i = 0; i < items.length(); i++) {
                
                JSONObject comment = items.optJSONObject(i);
                msg += "\n" + comment.optString("message") + "-- " + comment.optJSONObject("from").optString("name");
            }
               resultTextView.setText(msg);
           }
       });
}


[REST]

GET https://apis.live.net/v5.0/file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/comments?access_token=ACCESS_TOKEN

In the preceding code, change the photo ID to a different folder ID, file ID, album ID, photo ID, or video ID to get info about comments on another folder, file, album, photo, audio, or video. You can also specify a comment's ID to get info about that individual comment.

You can also do this.

  • Get a limited number of comments by using the limit parameter in the preceding code to specify the number of items to get. For example, to get the first two comments, use PHOTO_ID/comments?limit=2.
  • Set the first comments 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/comments?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, if they apply, to get the offset and limit parameter values of the previous and next entries, if they exist.

Top

Deleting comments

To delete a comment, use code like the following. The wl.skydrive_update scope is required.

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

[JavaScript and HTML for Windows Store apps]


function deleteComment_onClick() {
    WL.login({
        scope: "wl.skydrive_update"
    }).then(
        function (response) {
            WL.api({
                path: "comment.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131.A6B2A7E8F2515E5E!166",
                method: "DELETE"
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = "Comment deleted.";
                },
                function (responseFailed) {
                    document.getElementById("infoArea").innerText =
                        "Error calling API: " + responseFailed.error.message;
                }
            );
        },
        function (responseFailed) {
            document.getElementById("infoArea").innerText =
                "Error signing in: " + responseFailed.error_description;
        }
    );
}


[C# for Windows Store apps and Windows Phone apps]


private async void btnDeleteComment_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.DeleteAsync("comment.22688711f5410e6c.22688711f0410e6c!818.22688711F5410E6C!979");
        this.infoTextBlock.Text = "Comment deleted.";
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error deleting comment: " + exception.Message;
    }
}


[Objective-C for iOS]


-(void) deleteComment
{
    [self.liveConnectClient deleteWithPath:@"comment.1cfa02017372544c.1CFA02017372544C!209.1CFA02017372544C!222"
                                  delegate:self];        
}

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


[Java for Android]


public void deleteComment() {
    String commentId = "comment.2e82e8a5445fe036.2e82e8a5445fe036!571.2E82E8A5445FE036!578";
    client.deleteAsync(commentId , new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
            resultTextView.setText("Error deleting comment: " + exception.getMessage());
        }
        public void onComplete(LiveOperation operation) {
            resultTextView.setText("Comment deleted.");
        }
       });
}


[REST]

DELETE https://apis.live.net/v5.0/comment.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131.A6B2A7E8F2515E5E!160?access_token=ACCESS_TOKEN

In the preceding code, change the comment ID to the comment ID of the comment that you want to remove.

Top

Creating comments

To create a comment, use code like the following. The wl.skydrive_update scope is required.

Tip  You can create comments for items that are owned by, or shared with, the signed-in user:

  • To create a comment 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 comment 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.

[JavaScript and HTML for Windows Store apps]


function createComment_onClick() {
    WL.login({
        scope: "wl.skydrive_update"
    }).then(
        function (response) {
            WL.api({
                path: "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/comments",
                method: "POST",
                body: {
                    "message": "Nice picture!"
                }
            }).then(
                function (response) {
                    document.getElementById("infoArea").innerText = 
                        "Comment created. 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;
        }
    );
}


[C# for Windows Store apps and Windows Phone apps]


private async void btnCreateComment_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var commentData = new Dictionary<string, object>();
        commentData.Add("message", "Tulips for Springtime.");
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.PostAsync("file.8c8ce076ca27823f.8C8CE076CA27823F!129/comments", commentData);
        dynamic result = operationResult.Result;
        this.infoTextBlock.Text = "Comment created, ID: " + result.id;
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error creating comment: " + exception.Message;
    }
}


[Objective-C for iOS]


-(void) createComment
{
    NSDictionary * newComment = [[NSDictionary dictionaryWithObjectsAndKeys:
                                  @"nice pic!!", @"message",
                                  nil] retain];
        [self.liveConnectClient postWithPath:@"file.1cfa02017372544c.1CFA02017372544C!209/comments"
                                    dictBody:newComment                                    
                                    delegate:self];
}

- (void) liveOperationSucceeded:(LiveOperation *)operation
{
    NSDictionary *from = [operation.result objectForKey:@"from"];
    NSString *commentId = [operation.result objectForKey:@"id"];
    NSString *commentMessage = [operation.result objectForKey:@"message"];
    NSString *commentDate = [operation.result objectForKey:@"created_time"];
    NSString *userId = [from objectForKey:@"id"];
    NSString *userName = [from objectForKey:@"name"];
    self.displayLabel.text = [NSString stringWithFormat:
                              @"Comment Properties:\n"
                              @"\tId. %@\n"
                              @"\tUser Id. %@\n"
                              @"\tUser Name. %@\n"
                              @"\tMessage. %@\n"
                              @"\tCreated Time. %@\n"
                              , commentId,
                              userId, 
                              userName, 
                              commentMessage,
                              commentDate];
}


[Java for Android]


public void createComment() {
    final String commentsPath = "file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/comments";
    final LiveOperationListener opListener = new LiveOperationListener() {
        public void onError(LiveOperationException exception, LiveOperation operation) {
               resultTextView.setText("Error creating comment: " + exception.getMessage());
           }
        public void onComplete(LiveOperation operation) {
            JSONObject result = operation.getResult();
            String text = "Comment created:\nID = " + result.optString("id");
               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("message", "Nice picture!");
                       client.postAsync(commentsPath, body, opListener);    
                }
                catch(JSONException ex){
                    resultTextView.setText("Error creating comment: " + ex.getMessage());
                }
               }
        }
    );
}


[REST]

POST https://apis.live.net/v5.0/file.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!131/comments

Authorization: Bearer ACCESS_TOKEN
Content-Type: application/json

{
    "message": "Nice picture!"
}

In the preceding code, change the photo ID to a different folder ID, file ID, album ID, photo ID, or video ID, to add a comment to another folder, file, album, photo, or video.

Note  You cannot create a comment unless the associated resource (for example, the file or photo) is already shared.

For details about the required and optional structures that your app must provide to use POST, see the "Comment object" section in REST reference.

Top

 

 

Show:
© 2014 Microsoft