IMPORTANT: This version of Sitefinity CMS is out of support and the respective product documentation is no longer maintained and can be outdated. Use the version selector to view a supported product version.
Sitefinity CMS allows you to delete a comment through the Comments API.
When deleting a news comment, you must perform the following:
Get the news item.
First, get an instance of the master version of the news item that corresponds to the specified ID.
For more information about finding specific news items, see For developers: Query news items.
Get the live version.
The instance that corresponds to the ID argument is the master version of the news item. You can get the live version explicitly.
Get the comment.
Get the comment that corresponds to the specified ID.
Delete the comment.
After you get the comment, you delete it.
Save the changes.
Finally, you save all changes.
The following examples delete a news comment by its ID.
public
void
DeleteCommentByIdNativeAPI(Guid masterNewsId, Guid commentId)
{
NewsManager newsManager = NewsManager.GetManager();
// Check whether the news item exists
NewsItem news = newsManager.GetNewsItems().Where(nI => nI.Id == masterNewsId).FirstOrDefault();
if
(news !=
null
)
news = newsManager.Lifecycle.GetLive(news)
as
NewsItem;
// Get the comment
Comment comment = newsManager.GetComments().Where(c => c.CommentedItemID == news.Id && c.Id == commentId).SingleOrDefault();
(comment !=
// Delete the comment
newsManager.Delete(comment);
// Save the changes
newsManager.SaveChanges();
}
First, you get an instance of the NewsManager class. You get the specified news item by querying all items and filtering the collection by the ID property.
If the item exists, you get its live version. Then, you get the comment by calling GetComments and filtering based on theCommentedItemID and Id properties. Then, you delete the comment by calling the Delete method of the manager. Finally, you save the changes.
DeleteCommentByIdFluentAPI(Guid masterNewsId, Guid commentId)
int
newsCount = 0;
App.WorkWith().NewsItems().Where(nI => nI.Id == masterNewsId).Count(
out
newsCount);
(newsCount > 0)
var liveNewsItem = App.WorkWith().NewsItem(masterNewsId).GetLive();
(liveNewsItem !=
commentCount = 0;
// Check whether the comment exists
liveNewsItem.Comments().Where(c => c.Id == commentId).Count(
commentCount);
(commentCount > 0)
liveNewsItem.Comment(commentId).Delete().SaveChanges();
Then, you use the GetLive method of the singular facade to get the instance of the live version. You initialize the plural facade of the comments by calling Comments. Then, you get the comment by filtering based on the Id property. You delete the comment by calling theDelete method of the singular facade of the comment. Finally, you save the changes.
Back To Top
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important