Work with threads
Create a thread
When creating a thread, you must perform the following:
- Get an instance of the
Comment Serviceusing theGetCommentsServicemethod part of theSystemManager. - 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 aAuthorProxyclass which implements theIAuthorinterface. - To create a thread, you need to create a
ThreadProxy.
The constructor requires as parameters a title, item type, group key, author, and culture. - Create the thread using the
CreateThreadmethod 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 theGetThreadsmethod 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.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.