Create comments

You can create comments programmatically using the Comments API. To create a comment, you use the CreateComment method of ICommentService interface.

Create a new comment

You can create comments for any content item. In this example, you create a comment to a News item.
NOTE: When creating the comment, you must assign it to the Live version of the News item. For more information about news, see For developers: News.

To create the comment:

  1. Get an instance of the comments service using the SystemManager's GetCommentsService method.
  2. Link the News item with the comment by creating a comment thread.
    To link the specific News item with a new comment, you need to create a thread. Each News item has a different thread and only one thread. You create threads using the CreateThread method of the content service.

    NOTE: The method requires a thread proxy as a parameter.

  3. Create a thread proxy.
    You create new thread proxy by instantiating an object of the ThreadProxy class. The ThreadProxy constructor requires a thread title, type of the content item to which the proxy connects (in this case a NewsItem), a group key, and an author.

    You may also need to set the key of the thread proxy and language. The thread key is formed by the Live ID of the News item and the current culture, appended by an underscore.

  4. Create a new group to get the group key and pass the key as a parameter for the ThreadProxy.
    Each content type has exactly one group. In this case, you need to create a group for all comments related to the News content type. You create groups by using the CreateGroup method of the comments service.

    NOTE: The method above requires a group proxy as a parameter.

  5. Create a group proxy.
    You create a group proxy using the GroupProxy class. The group proxy requires you to set a group key. You can create the group key by using the GetUniqueProviderKey method of the ControlUtilities class. As you are creating a group for the News content type, you need to pass the full name of the NewsManager type and the name of the news manager provider.

    The group proxy requires a name, description and author. The author can be either a user retrieved using the AuthorProxy (you create the author proxy in next step), or the currently authenticated user.

  6. Create an author proxy.
    When creating a new comment, information is required about the author of the new comment. Therefore, you must use the AuthorProxy class which implements the IAuthor interface. When creating a new AuthorProxy, you can directly set the author name and author email properties.

    Sitefinity CMS also enables you to associate the new comment with existing user in case the comment author is already authenticated on your site.

  7. Create a comment proxy.
    To create a comment proxy, you use the CommentProxy class that is derived from the IComment interface. You can use the CommentProxy constructors to create a new comment with a specific message, thread key (described in step 3), and author (described in the previous step).
  8. Create a new comment
    To create a comment, you can use the CreateComment method of the comments service. This method receives as an argument a comment proxy that you created in the previous step.

Use the code below to create a News comment with the specified IDMessageAuthor name, email, and IP:

Change the status of a comment

To change the status of a comment, for example set a comment as Spam, you set the value of the Status property to the StatusConstants.Spam. The following example demonstrates creation of a Spam comment.

NOTE: The StatusConstants is an enumeration with four possible values: Published, WaitingForApproval, Spam, and Hidden.

Want to learn more?

Sign up for our free beginner training. Boost your credentials through advanced courses and certification.
Register for Sitefinity training and certification.

Was this article helpful?

Next article

Query comments