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 GetCommentsServicemethod.

  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 CreateThreadmethod 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 ThreadProxyclass. The ThreadProxyconstructor 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 Newscontent 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 GroupProxyclass. 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 NewsManagertype 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 CommentProxyclass 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:

C#
using System;
using System.Linq;
using Telerik.Sitefinity.Modules.News;
using Telerik.Sitefinity.News.Model;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments;
using Telerik.Sitefinity.Services.Comments.Proxies;
using Telerik.Sitefinity.Web.UI;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
   public partial class CommentsSnippets
   {
       public static IComment CreateNewsComment(Guid newsItemId, string content, IAuthor author, string ip)
       {
           NewsManager manager = NewsManager.GetManager();

           // Get the news item
           NewsItem newsItem = manager.GetNewsItem(newsItemId);

           //Gets an instance of the comment service
           var cs = SystemManager.GetCommentsService();
           var language = SystemManager.CurrentContext.Culture.Name;
           var threadKey = ControlUtilities.GetLocalizedKey(newsItemId, language);

           CommentsSnippets.EnsureNewsThreadExists(threadKey, author, newsItem.Title, manager, language, cs);

           //new comment is created via the CommentProxy
           var commentProxy = new CommentProxy(content, threadKey, author, ip);

           //set the status of the commentProxy to published. By default it is WaitingForApproval
           commentProxy.Status = StatusConstants.Published;

           var comment = cs.CreateComment(commentProxy);

           return comment;
       }

       /// <summary>
       /// Ensures the news thread exists.
       /// </summary>
       /// <param name="threadKey">The thread key.</param>
       /// <param name="author">The author.</param>
       /// <param name="threadTitle">The thread title.</param>
       /// <param name="manager">The manager.</param>
       /// <param name="language">The language.</param>
       /// <param name="cs">The cs.</param>
       private static void EnsureNewsThreadExists(string threadKey, IAuthor author, string threadTitle, NewsManager manager, string language, ICommentService cs)
       {
           ThreadFilter threadFilter = new ThreadFilter();
           threadFilter.ThreadKey.Add(threadKey);
           var thread = cs.GetThreads(threadFilter).SingleOrDefault();

           if (thread == null)
           {
               var groupKey = ControlUtilities.GetUniqueProviderKey(typeof(NewsManager).FullName, manager.Provider.Name);

               CommentsSnippets.EnsureNewsGroupExists(groupKey, author, cs);

               var culture = SystemManager.CurrentContext.AppSettings.GetCultureByName(language);

               var threadProxy = new ThreadProxy(threadTitle, typeof(NewsItem).FullName, groupKey, author, culture) { Key = threadKey };
               thread = cs.CreateThread(threadProxy);
           }
       }

       /// <summary>
       /// Ensures the news group exists.
       /// </summary>
       /// <param name="groupKey">The group key.</param>
       /// <param name="author">The author.</param>
       /// <param name="cs">The cs.</param>
       private static void EnsureNewsGroupExists(string groupKey, IAuthor author, ICommentService cs)
       {
           GroupFilter groupFilter = new GroupFilter();
           groupFilter.GroupKey.Add(groupKey);
           var group = cs.GetGroups(groupFilter).SingleOrDefault();

           if (group == null)
           {
               var groupProxy = new GroupProxy("Group title", "news items in provider", author) { Key = groupKey };
               group = cs.CreateGroup(groupProxy);
           }
       }
   }
}

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 Spamcomment.

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

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.