Deleting comments

Sitefinity allows you to delete a comment through the Comments API.

When deleting a news comment, you must perform the following:

  1. 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 Querying news items.

  2. 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.

  3. Get the comment.

    Get the comment that corresponds to the specified ID.

  4. Delete the comment.

    After you get the comment, you delete it.

  5. Save the changes.

    Finally, you save all changes.

Deleting a news comment by its ID

The following examples delete a news comment by its ID.

Native API

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();
 
        if (comment != null)
        {
            // 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.

For more information about finding specific news items, see Querying news items.

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.

Fluent API

public void DeleteCommentByIdFluentAPI(Guid masterNewsId, Guid commentId)
{
    int newsCount = 0;
 
    // Check whether the news item exists
    App.WorkWith().NewsItems().Where(nI => nI.Id == masterNewsId).Count(out newsCount);
 
    if (newsCount > 0)
    {
        // Check whether the news item exists
        var liveNewsItem = App.WorkWith().NewsItem(masterNewsId).GetLive();
 
        if (liveNewsItem != null)
        {
            int commentCount = 0;
 
            // Check whether the comment exists
            liveNewsItem.Comments().Where(c => c.Id == commentId).Count(out commentCount);
 
            if (commentCount > 0)
            {
                // Delete the comment
                liveNewsItem.Comment(commentId).Delete().SaveChanges();
            }
        }
    }
}

First, you use the plural facade of the news item to assure that the item with the specified Id exists.

For more information about finding specific news items, see Querying news items.

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.

See Also

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK