Work with threads

Create a thread

When creating a thread, you must perform the following:

  1. Get an instance of the Comment Service using the GetCommentsService method part of the SystemManager.
  2. Create an AuthorProxy.
    When creating comments, information is required about the author of the new comment, for example a name, email, and so on. Therefore, there is a  AuthorProxy class which implements the IAuthor interface.
  3. To create a thread, you need to create a ThreadProxy.
    The constructor requires as parameters a title, item type, group key, author, and culture.
  4. Create the thread using the CreateThread method as part of the comment service.

The following code creates a thread:

C#
using Telerik.Sitefinity.Security.Claims;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments.Proxies;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments
{
    public partial class CommentsSnippets
    {
        public void CreateThreadSnippet(string groupKey, string itemType, string title, string language)
        {
            var cs = SystemManager.GetCommentsService();
            var author = new AuthorProxy(ClaimsManager.GetCurrentUserId().ToString());

            var culture = SystemManager.CurrentContext.AppSettings.GetCultureByName(language);
            var threadProxy = new ThreadProxy(title, itemType, groupKey, author, culture);
            var thread = cs.CreateThread(threadProxy);
        }
    }
}

Query threads

Use any of the following options to retrieve threads:

  • Get a thread by threadKey:
    C#
    using System;
    using System.Collections.Generic;
    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.Threads
    {
        public partial class CommentsSnippets
        {
            public IThread GetThreadSnippet(string threadKey)
            {
                var cs = SystemManager.GetCommentsService();
                return cs.GetThread(threadKey);
            }
        }
    }
    
  • Get all threads by specific criteria.
    You must use the GetThreads method part of the comments service:
    C#
    using System.Collections.Generic;
    using Telerik.Sitefinity.Services;
    using Telerik.Sitefinity.Services.Comments;
    
    namespace SitefinityWebApp
    {
        public class CommentsSnippets2
        {
            public IEnumerable<IThread> GetAllThreads(string groupKey)
            {
                ICommentService cs = SystemManager.GetCommentsService();
                return cs.GetThreads(groupKey);
            }
            public IEnumerable<IThread> GetAllThreads2(ThreadFilter filter)
            {
                ICommentService cs = SystemManager.GetCommentsService();
                return cs.GetThreads(filter);
            }
    
            public IEnumerable<IThread> GetAllThreads3(ThreadFilter filter)
            {
                int totalCount = 0;
    
                ICommentService cs = SystemManager.GetCommentsService();
                
                return cs.GetThreads(filter, out totalCount);
            }
        }
    }

Modify threads

You can update existing thread properties using the UpdateThread method. The sample below demonstrates how to change the IsClosed property of the thread:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments.Threads
{
   public partial class CommentsSnippets
   {
       public void CloseThreadSnippet(string threadKey)
       {
           var cs = SystemManager.GetCommentsService();
           var thread = cs.GetThread(threadKey);
           thread.IsClosed = true;
           cs.UpdateThread(thread);
       }
   }
}

Delete threads

You can delete a thread using the DeleteThread method:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Modules.Comments.Threads
{
   public partial class ThreadsSnippets
   {
       public void DeleteThread(string threadKey)
       {
           var cs = SystemManager.GetCommentsService();
           cs.DeleteThread(threadKey);
       }
   }
}
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.