Subscribe to comments of blogs: Subscribe an email address to comments

After you create and Subscribe to comments of blogs: Register the new comment notifications strategy, you need to obtain the subscriber’s email address by using a form submit or a custom web service.

Obtain the thread key of the comments, using the blog post ID. Use the following sample:

C#
using System;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments;
using Telerik.Sitefinity.Web.UI;

namespace SitefinityWebApp
{
   public class GetThreadKey
   {
       public static string threadKey;
       private readonly Guid blogId = new Guid("4c53766c-a903-4e53-a9cd-d73e34c71980");

       public void GetThreadKeyByBlogPost(Guid blogPostId)
       {
           //gets an instance of the comments service
           var cs = SystemManager.GetCommentsService();

           //adds an instance to the comment filter
           var filter = new CommentFilter();

           //gets the thread related to the blog post
           var language = SystemManager.CurrentContext.Culture.Name;
           string getThreadKey = ControlUtilities.GetLocalizedKey(blogPostId, language);

           threadKey = getThreadKey;
       }
   }
}

Next, subscribe to the respective thread type by using the Subscribe method of the comment service that calls a method with the same signature from the currently registered ICommentNotificationsStrategy. To subscribe, copy the following code in the Bootstrapper.Bootstrapped Sitefinity event that you can use in the Global.asax file or, depending on your needs, in any widget, service, and so forth:

C#
using System;
using Telerik.Microsoft.Practices.Unity;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Blogs.Model;
using Telerik.Sitefinity.Data;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Comments.Notifications;

namespace SitefinityWebApp
{
   public class Global : System.Web.HttpApplication
   {
       protected void Application_Start(object sender, EventArgs e)
       {
           Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;
       }

       private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
       {
           ObjectFactory.Container.RegisterType<ICommentNotificationsStrategy, CustomCommentNotificationsStrategy>(new ContainerControlledLifetimeManager());

           var commentService = SystemManager.GetCommentsService();
           commentService.Subscribe(new CustomSubscriptionData()
           {
               SubscriberEmail = "test@mail.com",
               ThreadType = typeof(BlogPost).FullName,
               ThreadKey = GetThreadKey.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.
New to Sitefinity?