More in this section

Forums / Developing with Sitefinity / Subscribing Users to Forum Threads

Subscribing Users to Forum Threads

11 posts, 0 answered
  1. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    28 Aug 2009
    Link to this post
    Hello,

    I was wondering what the best method would be to add a subscription function to forum threads. A client has asked that their forums function, as a matter of fact, the same way that these Sitefinity forums work. When you reply to a thread, you have a checkbox above the post button which subscribes you to the thread, and if you choose so, any further replies on that thread are automatically e-mailed to you in an html format that matches the post.


    Thank you for your support,

    David
  2. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    31 Aug 2009
    Link to this post
    Any advice in regards to this question yet?

    Thank you,

    David
  3. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    02 Sep 2009
    Link to this post
    Hello David,

    The Sitefinity forums on our web site use different controls in order to achieve this functionality.

    Here is how I would implement such feature though. 

    1. Add a new profile field for each of your users. This is in case you use the standard membership provider.
    web.config:
     <profile defaultProvider="Sitefinity"
          <providers> 
            <clear /> 
            <add name="Sitefinity" connectionStringName="DefaultConnection" applicationName="/" type="Telerik.DataAccess.AspnetProviders.TelerikProfileProvider, Telerik.DataAccess" /> 
          </providers> 
          <properties> 
            <add name="LastPasswordChangedDate" /> 
            <add name="FirstName" /> 
            <add name="LastName" /> 
            <add name="Position" /> 
            <add name="Subscriptions" /> 
    ....... 

    In this property you will store each of the members subscriptions, for example in format {guid},{guid}, where the {guid} is the ID of the threads he follows. 

    The next step is to handle the forum post created event. The post objects has a ParentID property, which relates the post to the Thread ( in this case, the thread is the parent of the post). Once we handle the post created event, we can check the Thread's ID. Then we get all users and check the subscriptions property to match a subscription with the Thread ID. Once we find a match, we send the Post.Content as e-mail. 

    Global.asax
        void Application_Start(object sender, EventArgs e) 
        { 
            Telerik.Forums.ForumManager.Executed += new EventHandler<Telerik.ExecutedEventArgs>(ForumManager_Executed); 
        } 
     
        void  ForumManager_Executed(object sender, Telerik.ExecutedEventArgs e) 
        { 
            if (e.CommandName == "CreatePost"
            { 
               Telerik.Forums.ForumManager manager = new Telerik.Forums.ForumManager(); 
               Telerik.Forums.IPost post = manager.GetPost(e.ItemID); 
               //get all users, and their profile Subsriptions field. 
               //check if post.ParentID equals to any of the GUIDs in the Subscriptions collection 
               //if you find a match, send the e-mail(which can be taken from the user's profile as well) //with post.Content.   
            } 
                  
        } 


    You can add a checkbox to the template. When someone clicks the checkbox, you should add the current thread id to the user's profile (subscription property).

    This should do the trick. 

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  4. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    03 Sep 2009
    Link to this post
    Great!... well halfway!

    This solution is working well for every post I send from the back-end, but if I send one from the Forums control, it doesn't work!

    Any ideas why it would be this way?

    Thank you for your support,

    David
  5. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    03 Sep 2009
    Link to this post
    In addition, I'm having some issues with the checkbox. From code in /Sitefinity/ControlTemplates/Forums/EditPost.ascx, how can I reference the thread being posted to? There isn't an ID in the querystring for the thread, and there are no references to the post in the page either.


    Thanks,
    David
  6. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    08 Sep 2009
    Link to this post
    Hello David,

    Can you please double check that? The events are fired as expected when you create/delete/update threads and posts both through the backend, and the front end. As for the thread ID, isn't it an option to take it from the ExecutedEvent handler and the command arguments?

    Greetings,
    Georgi
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    08 Sep 2009
    Link to this post
    Thank you for your support.

    It turns out that it does work from the front-end as well as the back-end. I don't know why I was running into this problem previously. The problem with the checkbox is still persisting, however. This is what I have accomplished:

    I have added a checkbox to EditPost.ascx, and an OnClick event to the saveButton button. When the save button is clicked, it's supposed to get the current user, along with the ID of the thread that this post is going to be saved to. It will then get all of the user's subscriptions, and then add this one to the list if it isn't already there. I suppose, however, that this implementation does not make sense if it is a new thread, as there will obviously not be a thread in existence...

    With this in mind, how would it be best to implement this functionality?

    Thank you,
    David
  8. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    08 Sep 2009
    Link to this post
    Another issue with this system has reared it's ugly head.

    In addition, how can I get the checkbox I've added to EditPost.ascx to toggle its checked status based on whether or not they're already subscribing to the current thread? Once again, I'm unable to find out how to find the data for the current thread so that I can compare it to the list of the user's subscriptions.

    I hope I'm missing something huge here...

    Thanks again for your support,

    David
  9. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    10 Sep 2009
    Link to this post
    Hi David,

    Thank you for using our services.

    I think that Georgi forgot to mention that the command name for creating a new thread is UpdatePost. I know it is a little bit confusing but if you add it to your if statement it should work. Here is some sample code how to get the post, its information, and check user profile for subscription to that thread.

    Consider the following sample code bellow. I have first added an event to handle the button click for posting forum threads/replies. In this button I check the value of the checkbox and if it is true I add another key to the current session (this is added as C# script to EditPost.ascx):
    public void Page_Load(object sender, EventArgs e) 
        { 
            //subscribe to click event 
            saveButton.Click += new EventHandler(saveButton_Click); 
        } 
    void saveButton_Click(object sender, EventArgs e) 
        { 
            //if checked add key to current session 
            //similar logic can be implemented for unsubscribing 
            if (subscribe.Checked) 
                  HttpContext.Current.Session.Add("subscribe""true"); 
        } 

    Then as Georgi has sampled with the forums manager executing event handler implement some logic for subscribing the user that is posting the thread:
    void ForumManager_Executing(object sender, Telerik.ExecutingEventArgs e) 
        { 
           //CommandNames for management of posts are 
           //UpdatePost - create new thread or edit post 
           //CreatePost - reply to a thread 
           //DeletePost - delete post or thread 
           //add logic if command is delete to remove the thread ID from user subscriptions 
            if (e.CommandName == "UpdatePost" || e.CommandName == "CreatePost"
            { 
                Telerik.Forums.IPost currentPost = e.CommandArguments as Telerik.Forums.IPost; 
                string subscribe =""
                if (HttpContext.Current.Session["subscribe"]!=null
                    subscribe = HttpContext.Current.Session["subscribe"as string
                if (subscribe=="true"
                { 
                    if (currentPost != null
                    { 
                        //get relevant data for post 
                        //string currentPostTitle = currentPost.Title; 
                        string currentPostAuthor = currentPost.Owner; 
                        //string currentPostContent = currentPost.Content; 
                        Telerik.Forums.IForum parentForum = currentPost.ForumID; 
                        ProfileBase userProfile = ProfileBase.Create(currentPostAuthor); 
                        string subscriptions = userProfile.GetPropertyValue("Subscriptions"as string
                        //if it is not a new thread 
                        if (currentPost.Thread.ID!=null && !subscriptions.Contains(currentPost.Thread.ID.ToString())) 
                        { 
                            userProfile.SetPropertyValue("Subscriptions", subscriptions += currentPost.Thread.ID.ToString()); 
                            userProfile.Save(); 
                        } 
                        //if it is a new thread and has no parrent 
                        else if(!subscriptions.Contains(currentPost.ID.ToString())) 
                        { 
                            userProfile.SetPropertyValue("Subscriptions", subscriptions += currentPost.ID.ToString()); 
                            userProfile.Save(); 
                        }     
                    } 
                } 
            } 
        } 


    I hope that this sample will help you to implement the subscriptions functionality. Note that this is just sample and might need optimization.

    Best wishes,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  10. David
    David avatar
    50 posts
    Registered:
    14 Jan 2009
    10 Sep 2009
    Link to this post
    Thank you for your response.

    The code you posted is significantly more efficient than mine, and after implementing it, it works well. There is still the unresolved issue of checking the user's subscription status in the code of EditPost.ascx to toggle whether or no the checkbox should be checked.

    Currently, I have the checked status of the checkbox defaulting to true, and this is acceptable, but it would be nice to get this funcitonality.

    Thanks,
    David
  11. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    11 Sep 2009
    Link to this post
    Hi David,

    Thank you for the feedback.

    Could you, please send us the code for your EditPost.ascx, and a little bit more information on the use case scenario, plus your current version of Sitefinity. I will see what we can do to help you.

    Greetings,
    Radoslav Georgiev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
11 posts, 0 answered