Sitefinity 4 Programmatically add BlogPost Comments
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 );
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();
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();
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
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