Query comments
Sitefinity CMS enables you to query for specific comments.
Query a single comment
The following sample demonstrates how to query for a specific comment by its Key:
C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
public partial class GetCommentByKeySnippet
{
public static IComment GetCommentByKey(string commentKey)
{
// Gets an instance of the comments service
var cs = SystemManager.GetCommentsService();
//instantiate comment filter
var commentFilter = new CommentFilter();
commentFilter.CommentKey.Add(commentKey);
//get the comment with the specified commentKey
var comment = cs.GetComments(commentFilter).SingleOrDefault();
return comment;
}
}
}
In the code above, you:
- Get an instance of the comments service by using the
SystemManager's GetCommentsServicemethod. - Get an instance of the
CommentFilterclass. - Filter by comment key by adding the key in the
CommentKeycollection of theCommentFilter. - Get the comment that corresponds to the specified key.
If the method does not find any comments matching the filter criteria, null will be returned.
Query all comments by blog post
The following sample demonstrates how to query all blog post comments by the Id of the live version of the blog post item:
C#
using System;
using System.Collections.Generic;
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 IEnumerable<IComment> GetCommentsBySpecificBlogPost(Guid blogPostId)
{
//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 blog post
var language = SystemManager.CurrentContext.Culture.Name;
var threadKey = ControlUtilities.GetLocalizedKey(blogPostId, language);
//adds the thread key to the comments filter
filter.ThreadKey.Add(threadKey);
//retrieves all comments by specific blog post
var comments = cs.GetComments(filter);
return comments;
}
}
}
In the code above, you:
- Get an instance of the comment service by
SystemManager. - Get an instance of the comment filter that is retrieved by calling the constructor.
- Get the thread associated to the blog post.
Threads are the connection between a comment and the specific content item that is related to the comment. You can distinguish different threads by thread keys that are unique for each thread. The thread key is formed by the Live content item's ID and language. - Add the thread key to the comment filter.
- Return all comments matching the filter criteria using the
GetCommentsmethod of the comment service and passing the filter as a parameter.
Query all comments with a specific status and content item
You can also use CommentFilter to filter comments by different criteria, each one represented as a list:
AuthorIpAddressAuthorKeyCommentKeyGroupKeyHasRatingLanguageStatusThreadKeyThreadType
In the following example, you filter by more than one criteria:
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 IEnumerable<IComment> GetAllSpamCommentsForNews()
{
// Gets an instance of the comments service
var cs = SystemManager.GetCommentsService();
//instantiate comment filter
var commentFilter = new CommentFilter();
//add filter by Spam status
commentFilter.Status.Add(StatusConstants.Spam);
//add filter by News
commentFilter.ThreadType.Add(typeof(NewsItem).FullName);
//get the comment with the specified commentKey
var comments = cs.GetComments(commentFilter);
return comments;
}
}
}
In the code above, you:
- Get an instance of the comments service using the SystemManager
GetCommentsServicemethod. - Get an instance of the
CommentFilterclass that is used to compose a filter, based on which a collection ofICommentobjects is returned. - To filter by status Spam, add
StatusConstants.Spamin the status collection of thecommentFilter. - Get all comments that satisfy the filter criteria.
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.