Delete comments

Sitefinity CMS allows you to delete a single comment or a group of comments through the Comments API.

Delete a single comment by its key

When deleting a comment, you must perform the following:

  1. Get an instance of the comments service by using the SystemManager's GetCommentsService method.
  2. Delete the comment using the DeleteComment method of the comments service. The method expects the comment key as a parameter.

Consider the code sample below:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
    public partial class CommentsSnippes
    {
        public static void DeleteSingleComment(string commentKey)
        {
            //Gets an instance of the comments service
            var cs = SystemManager.GetCommentsService();

            //deletes comment by key. 
            //If the comment is not found - ItemNotFoundException will be thrown
            cs.DeleteComment(commentKey);
        }
    }
}

Delete multiple comments by their keys

When deleting multiple comments, follow the steps below:

  1. Get an instance of the comments service using the SystemManager's GetCommentsService method.
  2. Delete all comments using the DeleteCommentsmethod of the comments service. The methods expects a collection of comment keys as a parameter.

Consider the code sample below:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
    public partial class CommentsSnippets
    {
        public static void DeleteComments(IEnumerable<string> commentKeys)
        {
            //Gets an instance of the comments service
            var cs = SystemManager.GetCommentsService();

            //deletes comment by key. 
            //If any of the comments is not found - ItemNotFoundException will be thrown
            cs.DeleteComments(commentKeys);
        }
    }
}

Delete all comments for a specific content type

In case you  need to delete programmatically all comments for a specific content type, for example News, follow the steps below:

  1. Get an instance of the comments service.
    Use the GetCommentsServicemethod of the SystemManager.
  2. To find all comments related to News, you use the CommentFilterand add NewsItemfull type name as a criteria.
    Thus, you can use the GetComments method and pass the filter as an argument. For more information, see Query comments.
  3. Delete comments.
    After retrieving all keys of the comments, you can use the DeleteComments method.

Consider the code sample below:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
    public partial class CommentsSnippets
    {
        public static void DeleteAllCommentsForNews()
        {
            //gets an instance of the comments service
            var cs = SystemManager.GetCommentsService();

            //creates an instance of the comment filter
            var filter = new CommentFilter();
            filter.ThreadType.Add(typeof(NewsItem).FullName);

            //gets the keys of all comments that are related to News content type
            var commentsKeys = cs.GetComments(filter).Select(a => a.Key);

            //deletes the comments
            cs.DeleteComments(commentsKeys);
        }
    }
}

Delete all comments for a specific content item by the item's ID

You may need to programmatically delete all comments for a specific content item by the item's ID. In the following example, you delete all comments for a specific News item by its ID. Follow the steps below:

  1. Get an instance of the comments service.
    Use the GetCommentsService method of the SystemManager.
  2. To find all comments related to the specific News item, you use the ID of the News item and create a new thread key.
    Add the thread key in the CommentFilter and use the filter the get all comments. For more information, see Query comments.

Consider the code sample below:

C#
using System;
using System.Linq;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments;
using Telerik.Sitefinity.Web.UI;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
    public partial class CommentsSnippets
    {
        public static void DeleteAllCommentsForNewsItem(Guid newsItemId)
        {
            //gets an instance of the comments service
            var cs = SystemManager.GetCommentsService();

            //adds an instance to the comment filter
            var filter = new CommentFilter();

            //gets the thread related to the news item
            var language = SystemManager.CurrentContext.Culture.Name;
            var threadKey = ControlUtilities.GetLocalizedKey(newsItemId, language);

            //adds the thread key to the comments filter
            filter.ThreadKey.Add(threadKey);

            //retrieves all comments related to the news item
            var commentKeys = cs.GetComments(filter).Select(c => c.Key);

            //deletes comments
            cs.DeleteComments(commentKeys);
        }
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.