Last active
May 18, 2017 13:34
-
-
Save cbanner/3d7fc4df4362ad0603208cc60425a53e to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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