1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Getting comments from a blog post

Getting comments from a blog post

4 posts, 0 answered
  1. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    12 Nov 2012
    Link to this 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();
                    }
                }
    }
  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    12 Nov 2012
    Link to this post
    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;
    }

    Regards,
    Daniel
  3. Jeff
    Jeff avatar
    118 posts
    Registered:
    08 Jul 2011
    12 Nov 2012 in reply to Daniel Plomp
    Link to this post
    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?
  4. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    12 Nov 2012 in reply to Jeff
    Link to this post
    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);

    Regards,
    Daniel
4 posts, 0 answered