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>
@{
FacebookGetCommentsResponse response = Model.Comments.GetComments("115621461786836_1763586563656976");
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>
@{
FacebookFieldsCollection fields = new FacebookFieldsCollection {
FacebookCommentFields.From,
FacebookCommentFields.Message,
FacebookCommentFields.CreatedTime,
FacebookCommentFields.LikeCount
};
FacebookGetCommentsResponse response = Model.Comments.GetComments("115621461786836_1763586563656976", fields);
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>
@{
FacebookFieldsCollection fields = new FacebookFieldsCollection {
FacebookCommentFields.From,
FacebookCommentFields.Message,
FacebookCommentFields.CreatedTime,
FacebookCommentFields.LikeCount
};
FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
Identifier = "115621461786836_1763586563656976",
Fields = fields
};
FacebookGetCommentsResponse response = Model.Comments.GetComments(options);
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>
@{
FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
Identifier = "115621461786836_1763586563656976",
IncludeSummary = true,
Limit = 0
};
FacebookGetCommentsResponse response = Model.Comments.GetComments(options);
FacebookCommentsSummary summary = response.Body.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>
@{
FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
Identifier = "115621461786836_1763586563656976",
Filter = FacebookCommentsFilter.Stream
};
FacebookGetCommentsResponse response = Model.Comments.GetComments(options);
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>
@{
FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
Identifier = "115621461786836_1763586563656976",
Order = FacebookCommentsOrder.Chronological
};
FacebookGetCommentsResponse response = Model.Comments.GetComments(options);
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>
@{
FacebookGetCommentsOptions options = new FacebookGetCommentsOptions {
Identifier = "115621461786836_1763586563656976",
Limit = 50
};
FacebookGetCommentsResponse response1 = Model.Comments.GetComments(options);
options.After = response1.Body.Paging.Cursors.After;
FacebookGetCommentsResponse response2 = Model.Comments.GetComments(options);
List<FacebookComment> comments = new List<FacebookComment>();
comments.AddRange(response1.Body.Data);
comments.AddRange(response2.Body.Data);
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.