Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
This Gist is a companion to a post on the Episerver Social blog called "Getting Started with Comments": http://world.episerver.com/blogs/chris-banner/dates/2017/5/getting-started-with-comments/. It demonstrates the fundamentals of working with Comments in Episerver Social.
public class MyPageController : ContentController<MyContent>
{
private readonly ICommentService commentService;
public MyPageController()
{
commentService = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<ICommentService>();
}
// ...
private void SubmitComment(PageData page, string body)
{
// 1. Construct the comment, defining its Body, Author, and Parent
var comment = new Comment(
CreateReferenceFor(page),
CreateReferenceFor(HttpContext.Current.User.Identity),
body,
true
);
// 2. Submit the comment to Episerver Social.
commentService.Add(comment);
}
private void SubmitReply(CommentId parentCommentId, string body)
{
// Convert the CommentId of the parent Comment into a Reference.
var parentCommentReference = parentCommentId.ToReference();
// Construct the comment, defining its Body, Author, and Parent
var comment = new Comment(
parentCommentReference,
CreateReferenceFor(HttpContext.Current.User.Identity),
body,
true
);
// Submit the comment to Episerver Social.
commentService.Add(comment);
}
private ResultPage<Comment> RetrieveComments(Reference parent, int pageOffset = 0, int pageSize = 10)
{
// Define the criteria by which Comments should be retrieved
var criteria = new Criteria<CommentFilter>()
{
Filter = new CommentFilter
{
Parent = parent
},
PageInfo = new PageInfo
{
PageOffset = pageOffset,
PageSize = pageSize
}
};
// Retrieve the Comments from Episerver Social
return commentService.Get(criteria);
}
private ResultPage<Comment> RetrieveAllComments(PageData page, int pageSize = 10)
{
// Define the criteria by which Comments should be retrieved
var criteria = new Criteria<CommentFilter>()
{
Filter = new CommentFilter
{
Ancestor = CreateReferenceFor(page)
},
PageInfo = new PageInfo
{
PageSize = pageSize
}
};
// Retrieve the Comments from Episerver Social
return commentService.Get(criteria);
}
private Reference CreateReferenceFor(PageData page)
{
return Reference.Create(page.ContentGuid.ToString());
}
private Reference CreateReferenceFor(IIdentity user)
{
return user.IsAuthenticated
? Reference.Create(user.Name)
: Reference.Create("Anonymous");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment