BlogPost.Comments.Count is always 0
I'm trying to build a custom widget which shows the "most discussed" blog posts. This will work by counting the number of comments and displaying them ordered.
Whenever use the fluent api to get blog posts, the .Comments.Count property is always 0. I don't understand what I'm doing wrong.
My site has 2 blogs, with 7 posts, 2 of which have comments.
System.Collections.Generic.List<
BlogPost
> blogposts = App.WorkWith().BlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live).Get().ToList();
foreach (BlogPost blogpost in blogposts)
BlogPost bp = App.WorkWith().BlogPost(blogpost.OriginalContentId).GetLive().Get();
int comment_count = bp.Comments.Count;
Hi Raymond,
i think you don't do anything wrong. Here is what the Sitefinity Support told me a few days ago: "...it seems that Comments.Count property is not working as expected...".
You have to use some kind of workaround. Maybe retrieve all comments for that blog and then go on with your specific tasks:
BlogsManager blogsManager = BlogsManager.GetManager();
var allComments = blogsManager.GetComments().Where(c => c.ParentGroupIds.Contains(MyBlogID)).ToList();
Hello Raymond,
Please let us know if your issue was resolved. Thanks so much.
Regards,
Grace Hallwachs
the Telerik team
Hi Grace,
Thanks Matthias, that worked for me. For anyone else to come across this, my working code is:
System.Collections.Generic.List<
BlogPost
> blogposts = App.WorkWith().BlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live).Get().ToList();
foreach (BlogPost blogpost in blogposts)
BlogsManager blogsManager = BlogsManager.GetManager();
var allComments = blogsManager.GetComments().Where(c => c.ParentGroupIds.Contains(blogpost.Id)).ToList();
Hello Daniel,
Yes, you can use the Comments property from the BlogPostItem. I have included a code sample that retrieves all the live blog posts and then counts all the comments.
public
void
GetBlogsComments()
BlogsManager blogsManager = BlogsManager.GetManager();
//Get the master version.
BlogPost blogPost = blogsManager.GetBlogPosts().Where(b => b.Status == ContentLifecycleStatus.Live).FirstOrDefault();
if
(blogPost !=
null
)
// Get the live version
blogPost = blogsManager.Lifecycle.GetLive(blogPost)
as
BlogPost;
// Get the comment
var aaa = blogsManager.GetComments().ToList();
aaa.Count();
Is this bug going to be fixed? We are running into the same problem and do not want to have to make redundant calls to the database just to get the comments count if we already have the blog post object: dataItem.Comments.Count always shows zero.
This does not appear to work properly in sf6.3.5000. I attempt to use blogPost.Comments.Count and it's always 0.