Getting comments from a blog post
Can someone tell me why the following isn't returning any comments in any of my blog posts. Tried it several different ways and it's always the same result... post.Comments.Count is always 0.
I'm using SF 5.1.
Thanks
using (var fluent = App.WorkWith())
var posts = App.WorkWith().BlogPosts().Get();
if (posts.Count() > 0)
var listofAllComments = new List<
Comment
>();
foreach (BlogPost post in posts)
if (post.Comments.Count > 0)
foreach (Comment c in post.Comments)
listofAllComments.Add(c);
if (listofAllComments != null)
rptComments.DataSource = listofAllComments;
rptComments.DataBind();
Hi Jeff,
This is a bug in Sitefinity. There is no PITS item for it.
You can get the comments count by using the following code:
private
int
GetBlogsCommentsCount(BlogPost post)
// Using this method because the Comments property on a post always returns 0
int
result = 0;
var blogsManager = BlogsManager.GetManager();
if
(post !=
null
)
// Get the live version
post = blogsManager.Lifecycle.GetLive(post)
as
BlogPost;
result = blogsManager.GetComments().Count();
return
result;
Hey Daniel, thanks for the reply. I'm actually trying to get the comments themselves, not the count. If I remove the if (post.Comments.Count > 0) I still don't get any comments so the bug must extend further than just with the count.
There must be another way to retrieve the comments. Can someone please enlighten me?
Hi Jeff,
Yes, I know. What I actually meant is that the whole 'Comments' property is not working.
You can try to get the Comments directly and have it filtered by the ContentItem, in this case the BlogPost?
Something like this:
BlogsManager m = BlogsManager.GetManager();
m.GetComments().Where(c => c.CommentedItemID == post.ID);