Sitefinity 4 Programmatically add BlogPost Comments

Posted by Community Admin on 04-Aug-2018 12:52

Sitefinity 4 Programmatically add BlogPost Comments

All Replies

Posted by Community Admin on 06-May-2011 00:00

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

Posted by Community Admin on 06-May-2011 00:00

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

Posted by Community Admin on 06-May-2011 00:00

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

Posted by Community Admin on 11-May-2011 00:00

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

This thread is closed