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:
- Get an instance of the comments service by using the SystemManager's
GetCommentsServicemethod. - Delete the comment using the
DeleteCommentmethod 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:
- Get an instance of the comments service using the SystemManager's
GetCommentsServicemethod. - 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:
- Get an instance of the comments service.
Use theGetCommentsServicemethod of theSystemManager. - To find all comments related to News, you use the
CommentFilterand addNewsItemfull type name as a criteria.
Thus, you can use theGetCommentsmethod and pass the filter as an argument. For more information, see Query comments. - Delete comments.
After retrieving all keys of the comments, you can use theDeleteCommentsmethod.
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:
- Get an instance of the comments service.
Use theGetCommentsServicemethod of the SystemManager. - 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 theCommentFilterand 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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.