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:    

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:

In the code above, you:

  1. Get an instance of the comment service by SystemManager.
  2. Get an instance of the comment filter that is retrieved by calling the constructor.
  3. 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.
  4. Add the thread key to the comment filter.
  5. Return all comments matching the filter criteria using the GetComments method 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:

  • AuthorIpAddress
  • AuthorKey
  • CommentKey
  • GroupKey
  • HasRating
  • Language
  • Status
  • ThreadKey
  • ThreadType

In the following example, you filter by more than one criteria:

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.

Was this article helpful?

Next article

Modify comments