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:
1. Get an instance of the comments service by using the `SystemManager's GetCommentsService` method.
2. Get an instance of the `CommentFilter` class.
3. Filter by comment key by adding the key in the `CommentKey` collection of the `CommentFilter`.
4. 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
//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:
1. Get an instance of the comments service using the SystemManager `GetCommentsService` method.
2. Get an instance of the `CommentFilter` class that is used to compose a filter, based on which a collection of `IComment` objects is returned.
3. To filter by status *Spam*, add `StatusConstants.Spam` in the status collection of the `commentFilter`.
4. Get all comments that satisfy the filter criteria.