Getting a post
If you already know the ID of a given Facebook post, you can use the GetPost
method to get further information about the post - this can be done like:
@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
@{
FacebookGetPostResponse response = Model.Posts.GetPost("185238545407_10158539914495408");
FacebookPost post = response.Body;
<p>ID: @post.Id</p>
<p>Message: @post.Message</p>
<p>Story: @post.Story</p>
<p>Created: @post.CreatedTime</p>
}
Fields
Like the rest of the Graph API, Facebook will only respond with a number of fields by default. For a post, this will be the created_time
, message
, story
and id
fields.
To request more fields, you can pass a second parameter to the GetPost
method with the type of either FacebookFieldsCollection
or FacebookField
:
@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Constants
@using Skybrud.Social.Facebook.Fields
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
@{
FacebookFieldsCollection fields = new FacebookFieldsCollection(
FacebookPostFields.Id,
FacebookPostFields.Message,
FacebookPostFields.Story,
FacebookPostFields.CreatedTime,
FacebookPostFields.From
);
FacebookGetPostResponse response = Model.Posts.GetPost("185238545407_10158539914495408", fields);
FacebookPost post = response.Body;
<p>ID: @post.Id</p>
<p>Message: @post.Message</p>
<p>Story: @post.Story</p>
<p>Created: @post.CreatedTime</p>
<p>From: @post.From.Name</p>
}
Since we're now specifically requesting the from
field, we can then also read the author of the post via the post.From
property, as it now has a value in the underlying JSON.
Getting the likes of a post
Due to Facebook's change to their privacy policy, you will not be able to get a list of the users that has liked a given post. You may however request a summary of all likes the post has received.
This is done by requesting the likes.limit(0).summary(true)
field:
@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Options.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
@{
FacebookGetPostResponse response = Model.Posts.GetPost(new FacebookGetPostOptions {
Identifier = "185238545407_10158539914495408",
Fields = "likes.limit(0).summary(true)"
});
FacebookPost post = response.Body;
<p>ID: @post.Id</p>
<p>Likes: @post.Likes.Summary.TotalCount</p>
<hr />
<pre>@post.JObject</pre>
}
The "special" field used here instructs the Graph API to return the likes, but set the limit to zero, and instead include the summary.
Getting the comments of a post
In a similar way as we could get a summary about the likes of a post, we can also get a summary about the comments the post has received - this time by requesting the comments.limit(0).summary(true)
field:
@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Options.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
@{
FacebookGetPostResponse response = Model.Posts.GetPost(new FacebookGetPostOptions {
Identifier = "185238545407_10158539914495408",
Fields = "comments.limit(0).summary(true)"
});
FacebookPost post = response.Body;
<p>ID: @post.Id</p>
<p>Comments: @post.Comments.Summary.TotalCount</p>
<hr />
<pre>@post.JObject</pre>
}
Technically you can remove the part of the field that specified the limits, in which case the Graph API will include a list of the comments. But again due to Facebook's recent privacy changes, this list will not include any user information.
Getting the amount of times a post has been shared
By requesting the shares
fields, you can also get the amount of times a given post has been shared:
@using Skybrud.Social.Facebook
@using Skybrud.Social.Facebook.Models.Posts
@using Skybrud.Social.Facebook.Options.Posts
@using Skybrud.Social.Facebook.Responses.Posts
@inherits WebViewPage<FacebookService>
@{
FacebookGetPostResponse response = Model.Posts.GetPost(new FacebookGetPostOptions {
Identifier = "185238545407_10158539914495408",
Fields = "shares"
});
FacebookPost post = response.Body;
<p>ID: @post.Id</p>
<p>Shares: @post.Shares.Count</p>
<hr />
<pre>@post.JObject</pre>
}