More in this section

Forums / Bugs & Issues / Sitefinity 4 Programmatically add BlogPost Comments

Sitefinity 4 Programmatically add BlogPost Comments

4 posts, 0 answered
  1. Bart
    Bart avatar
    8 posts
    Registered:
    17 Feb 2011
    06 May 2011
    Link to this post
    Hi There awesome people, 

    We are trying to programmatically add comments to blog posts.
    Our code does not show any errors but the comments are not added to the blog posts

    Here is our code

    BlogPost blogPost = App.WorkWith().BlogPosts().Where( b => b.Title == "New Blog Post" ).Get().First();
     
    BlogsManager bManager = BlogsManager.GetManager();     
     
    Comment comment = bManager.CreateComment( blogPost );
    comment.AuthorName = "Admin";
    comment.Content = "Content";
    comment.Title = "Test";
    comment.Owner = Guid.Empty;
    comment.Email = "email@domain.com";
    comment.CommentStatus = CommentStatus.Published;
                             
    blogPost.Comments.Add( comment );

    I have looked for some way to maybe save the blogpost, but could not find anything.
    I have checked the backend and it does not show there either

    Many Thanks,
    Bart Kooi
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    06 May 2011
    Link to this post
    Hello Bart,

    Here is a sample that shows how to create a commment

    var manager = BlogsManager.GetManager();
    var provider = (ContentDataProviderBase)manager.Provider;
    var comment = provider.CreateComment((ICommentable)contentItem);
    comment.Content = "this is the content of my comment";
    comment.AuthorName = SecurityManager.GetCurrentUserName();
    comment.Website = "";
    comment.Email = "";
    comment.IpAddress = "";
    comment.Owner = SecurityManager.GetCurrentUserId();
    comment.CommentStatus = CommentStatus.Published;
    manager.SaveChanges();


    here is the fluent api way

    var id = Guid.NewGuid();
    var fluent = App.WorkWith();
     fluent.ContentItem(id)
      .GetLive()
      .Comment()
      .CreateNew()
      .Do(c =>
      {
        c.Content = "test";
        c.AuthorName = "test";
        c.DateCreated = DateTime.UtcNow;
        c.IpAddress = "";
        c.Email ="";
        c.Website = "";
        c.Visible = true;
      })
      .Done();
     fluent.Dispose();


    Greetings,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Bart
    Bart avatar
    8 posts
    Registered:
    17 Feb 2011
    06 May 2011
    Link to this post
    Hi,

    Thanks for the quick reply.
    I have used your code and it adds a comment, the problem now is it does not link to the blog post

    I have attached a screenshot with the comments, when I click on Link to commented item, it just refreshes the page.

    My code looks like this now

    BlogPost blogPost = App.WorkWith().BlogPosts().Where( b => ( b.Parent.Title == "Regulatory Blog" ) && ( b.Title == "New Blog Post" ) ).Get().First();
     
    var manager = BlogsManager.GetManager();
    var provider = ( ContentDataProviderBase )manager.Provider;
    var comment = provider.CreateComment( ( ICommentable )blogPost );
     
    comment.Content = "this is the content of my comment";
    comment.AuthorName = "Anonymous";
    comment.Website = "domain.com";
    comment.Email = "user@domain.com";
    comment.IpAddress = "";
    comment.Owner = SecurityManager.GetCurrentUserId();
    comment.CommentStatus = CommentStatus.Published;
     
    manager.SaveChanges();

    I have also attached a screenshot of my blogs to show that the blog does exist

    Many Thanks
    Bart Kooi
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    11 May 2011
    Link to this post
    Hello Bart,

    Can you try to set CommentedItemID property  ( Guid of the content item ) of the Comment object explicitly in the code.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
4 posts, 0 answered