Facebook Getting a list of comments

Getting a list of comments

To get a list of comments of a Facebook object - eg. a post, album, event or even another comment - you can use the GetComments method in the comments endpoint. This method can be used like shown below:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>
              
@{

    // Make the call to the Facebook Graph API
    FacebookGetCommentsResponse response = Model.Comments.GetComments("115621461786836_1763586563656976");
    
    // Iterate over the comments
    foreach (FacebookComment comment in response.Body.Data) {

        <pre><strong>@comment.Id</strong></pre>
        <pre>@comment.From.Name</pre>
        <pre>@comment.CreatedTime.ToRfc2822</pre>
        <pre>@comment.Message</pre>

    }

}

Options

Like shown above, the GetComments method supports specifying just the ID of the parent object. The method has a number of overloads - eg. to support more advanced scenarios, you can specify an instance of FacebookGetCommentsOptions.

Fields

The Graph API will only return the id, from, created_time and message fields by default. To return even more fields, you can supply an instance of FacebookFieldsCollection as the second parameter:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Constants
@using Skybrud.Social.Facebook.Fields
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>
              
@{

    // Initialize a collection of the fields to be returned
    FacebookFieldsCollection fields = new FacebookFieldsCollection {
        FacebookCommentFields.From,
        FacebookCommentFields.Message,
        FacebookCommentFields.CreatedTime,
        FacebookCommentFields.LikeCount
    };

    // Make the call to the Facebook Graph API
    FacebookGetCommentsResponse response = Model.Comments.GetComments("115621461786836_1763586563656976", fields);

    // Iterate over the comments
    foreach (FacebookComment comment in response.Body.Data) {

        <pre><strong>@comment.Id</strong></pre>
        <pre>@comment.From.Name</pre>
        <pre>@comment.CreatedTime.ToRfc2822</pre>
        <pre>@comment.Message</pre>
        <pre>@comment.LikeCount</pre>

    }

}

The FacebookCommentFields class is a static class with constants for known fields of a comment.

You can also specify the fields to be returned by supplying an instance of FacebookGetCommentsOptions and setting the Fields property:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Constants
@using Skybrud.Social.Facebook.Fields
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Options.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>
              
@{

    // Initialize a collection of the fields to be returned
    FacebookFieldsCollection fields = new FacebookFieldsCollection {
        FacebookCommentFields.From,
        FacebookCommentFields.Message,
        FacebookCommentFields.CreatedTime,
        FacebookCommentFields.LikeCount
    };

    // Initialize the options for the call to the API
    FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
        Identifier = "115621461786836_1763586563656976",
        Fields = fields
    };

    // Make the call to the Facebook Graph API
    FacebookGetCommentsResponse response = Model.Comments.GetComments(options);

    // Iterate over the comments
    foreach (FacebookComment comment in response.Body.Data) {

        <pre><strong>@comment.Id</strong></pre>
        <pre>@comment.From.Name</pre>
        <pre>@comment.CreatedTime.ToRfc2822</pre>
        <pre>@comment.Message</pre>
        <pre>@comment.LikeCount</pre>

    }

}

To get a better understanding of fields in general, you can have a look at the fields page for more information.

Modifiers

The /{object-id}/comments edge supports two different modifiers, which are described below. Each modifier will help determining the data returned by the Graph API.

Summary

While the GetComments method typically returns a paginated list of comments, you can also request a summary about the total number of comments a given comment has received. In Skybrud.Social, you can do this by setting the IncludeSummary property to true like shown below:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Options.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>

@{

    // Initialize the options for the call to the API
    FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
        Identifier = "115621461786836_1763586563656976",
        IncludeSummary = true,
        Limit = 0
    };

    // Make the call to the Facebook Graph API
    FacebookGetCommentsResponse response = Model.Comments.GetComments(options);
    
    // Get a reference to the summary object
    FacebookCommentsSummary summary = response.Body.Summary;

    // Summarize the summary
    <pre>@summary.Order</pre>
    <pre>@summary.TotalCount</pre>
    <pre>@summary.CanComment</pre>

}

When requesting a summary of the comments, we most likely don't care about the actual list of comments, in which case we can set the Limit property to 0.

The summary is represented by an instance of FacebookCommentsSummary, where you can get the total number of comments from the TotalCount property.

Filter

The Graph API will only return top-level comments by default. To get all comments (including replies), you can again specify an instance FacebookGetCommentsOptions and then set the Filter property:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Options.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>

@{

    // Initialize the options for the call to the API
    FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
        Identifier = "115621461786836_1763586563656976",
        Filter = FacebookCommentsFilter.Stream
    };

    // Make the call to the Facebook Graph API
    FacebookGetCommentsResponse response = Model.Comments.GetComments(options);

    // Iterate over the comments
    foreach (FacebookComment comment in response.Body.Data) {

        <pre><strong>@comment.Id</strong></pre>
        <pre>@comment.From.Name</pre>
        <pre>@comment.CreatedTime.ToRfc2822</pre>
        <pre>@comment.Message</pre>
        <pre>@comment.LikeCount</pre>

    }

}

The FacebookCommentsFilter enum class supports two different values - Stream representing all comments and TopLevel (default) representing all top-level comments.

Ordering

The default sort order may depend on both the filter modifier/option and the parent object the comments belong to. To control the sort order, you can specify an instance of FacebookGetCommentsOptions like shown below:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Options.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>
              
@{
    
    // Initialize the options for the call to the API
    FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
        Identifier = "115621461786836_1763586563656976",
        Order = FacebookCommentsOrder.Chronological
    };

    // Make the request to the API
    FacebookGetCommentsResponse response = Model.Comments.GetComments(options);
    
    // Iterate over the comments
    foreach (FacebookComment comment in response.Body.Data) {

        <pre><strong>@comment.Id</strong></pre>
        <pre>@comment.From.Name</pre>
        <pre>@comment.CreatedTime.ToRfc2822</pre>
        <pre>@comment.Message</pre>

    }

}

FacebookCommentsOrder is an enum class with the values Ranked , Chronological, ReverseChronological and Unspecified (default).

Pagination

The /{object-id}/comments edge will initially return the 25 first comments. To get more results, or get additional pages, you can update the Limit and After properties in the options instance:

@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Comments
@using Skybrud.Social.Facebook.Options.Comments
@using Skybrud.Social.Facebook.Responses.Comments
@inherits WebViewPage<FacebookService>
              
@{

    // Initialize the options for the call to the API
    FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
        Identifier = "115621461786836_1763586563656976",
        Limit = 50
    };

    // Make the call to the Facebook Graph API for the first page
    FacebookGetCommentsResponse response1 = Model.Comments.GetComments(options);

    // Update the "after" cursor in the options
    options.After = response1.Body.Paging.Cursors.After;

    // Make the call to the Facebook Graph API for the second page
    FacebookGetCommentsResponse response2 = Model.Comments.GetComments(options);

    // Add comments from both responses to a common list
    List<FacebookComment> comments = new List<FacebookComment>();
    comments.AddRange(response1.Body.Data);
    comments.AddRange(response2.Body.Data);

    // Iterate over the comments
    foreach (FacebookComment comment in comments) {
        <pre><strong>@comment.Id</strong></pre>
    }

}

In this example, we initially raise the limit to 50, and make the request to the API for the first page. That response will - apart from the 50 first comments - contain pagination information. The /{object-id}/comments edge what is called cursor-based pagination, and the information therefore contains an After cursor which we use to update the options, and then make a new request to get the next 50 comments.