1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Require Login for Blogging

Require Login for Blogging

18 posts, 0 answered
  1. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    10 Jul 2008
    Link to this post
    What would be the best way for me to require Login access for leaving comments.

    Thanks,
    Chris
  2. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    11 Jul 2008
    Link to this post
    Hi Chris,

    To achieve the required functionality, you need to create your own control that extends the base class - Telerik.Blogs.WebControls.BlogCommentsList. Here are some directions on how to do that with Sitefinity:

    1.
    In the CreateChildControls method of the BlogCommentsListExtender, subscribe Submit button to the Click event and then add an event handler:

    protected override void CreateChildControls() 
            { 
                if (this.Page == null
                    this.Page = new Page(); 
     
                base.CreateChildControls(); 
     
                this.Container.SubmitButton.Click += new EventHandler(SubmitButton_Click)
            } 

    2.
    In the event handler, check whether the user is authenticated:

    void SubmitButton_Click(object sender, EventArgs e) 
            { 
                if (!this.Page.User.Identity.IsAuthenticated) 
                { 
                    this.ParentID = Guid.Empty; 
                    this.Page.Response.Redirect("~/login.aspx"); 
                } 
            } 

    3. Modify ~/Sitefinity/ControlTemplates/Blogs/ContentViewSingleItem.ascx template to use BlogCommentsListExtender instead of the old
    BlogCommentsList custom control.

    ... 
    <%@ Register Assembly="App_Code" Namespace="Telerik.Blogs.WebControls" TagPrefix="sfWeb" %> 
    ... 
     
    <sfWeb:BlogCommentsListExtender ID="commentsList" runat="server" CssClass="sf_commentsList"
            ... 
    </sfWeb:BlogCommentsListExtender> 

    For your convenience I have attached the full example
    .

    Hope this helps.

    Greetings,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  3. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    11 Jul 2008
    Link to this post
    I tried to implement this and it is saying The BlogCommentsList does not exist in the namespace Telerik.Blogs.Webcontrols.  Has this changed in 3.2 sp2.
  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    14 Jul 2008
    Link to this post
    Hi Chris,

    BlogCommentsList control is available in Sitefinity v3.2 SP2 and later. To achieve the required functionality in earlier versions, you need to override CommentsList base class instead of BlogCommentsList:

    using Telerik.Cms.Engine.WebControls; 
     
    namespace Telerik.Cms.Engine.WebControls 
        public class CommentsListExtender : CommentsList 
        { 
            protected override void CreateChildControls() 
            { 
                if (this.Page == null
                    this.Page = new Page(); 
     
                base.CreateChildControls(); 
     
                this.Container.SubmitButton.Click += new EventHandler(SubmitButton_Click);  
            } 
     
            void SubmitButton_Click(object sender, EventArgs e) 
            { 
                if (!this.Page.User.Identity.IsAuthenticated) 
                { 
                    this.ParentID = Guid.Empty; 
                    this.Page.Response.Redirect("~/login.aspx"); 
                } 
            } 
        } 

    Do let us know if you need any further assistance.

    Greetings,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  5. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    15 Jul 2008
    Link to this post

    I took the code that you gave me and made it where the submit button is just disabled if the user is not logged in.  It also gives them a message that they must be logged in to comment.

    I am having a little trouble automatically providing the username and email to the CommentsListContainer.

    Using the following code works for the first comment a user might leave but this method doesn't run after post back.  So the text boxes are empty again.  Where could I set these values to have them fill after post back as well.

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using Telerik.Cms.Engine;  
    using Telerik.Cms.Engine.WebControls;  
    using Telerik.News;  
     
    /// <summary>  
    /// Summary description for CommentsListExtender  
    /// </summary>  
    namespace MyControls  
    {  
     
        public class CommentsListExtender : CommentsList  
        {  
            public CommentsListExtender()  
            {  
                  
                // TODO: Add constructor logic here  
                //  
            }  
     
            public override string ProviderName  
            {  
                get 
                {  
                    if (string.IsNullOrEmpty(base.ProviderName))  
                        return NewsManager.DefaultContentProvider;  
     
                    return base.ProviderName;  
                }  
                set 
                {  
                    base.ProviderName = value;  
                }  
            }  
     
            protected override void CreateChildControls()  
            {  
                object obj = this.Context.Items["NewsItem"];  
                if (obj != null)  
                    this.articleId = (Guid)((Telerik.Web.UrlRewriteData)obj).Data;  
                if (this.articleId == Guid.Empty)  
                    return;  
     
                base.ParentID = articleId;  
                base.Moderation = false;  
     
                base.CreateChildControls();  
     
                //this.Container.SubmitButton.Click += new EventHandler(SubmitButton_Click);  
                BuildComments();  
            }  
     
            protected override void OnLoad(EventArgs e)  
            {  
                base.OnLoad(e);  
     
            }  
     
            void BuildComments()  
            {  
                if (!this.Page.User.Identity.IsAuthenticated)  
                {  
                    ((Button)this.Container.FindControl("submitBtn")).Enabled = false;  
                    this.Container.FindControl("lblNotLoggedIn").Visible = true;  
                }  
                else 
                {  
                    ((Button)this.Container.FindControl("submitBtn")).Enabled = true;  
                    this.Container.FindControl("lblNotLoggedIn").Visible = false;  
                    ((TextBox)this.Container.FindControl("authorTxt")).Text = Membership.GetUser().UserName.ToString();  
                    ((TextBox)this.Container.FindControl("emailTxt")).Text = Membership.GetUser().Email.ToString();  
                }  
            }  
     
     
            private Guid articleId;  
        }  


    Thanks,
    Chris
  6. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    17 Jul 2008
    Link to this post
    Hello Chris,

    Actually, the base CommentsList class has a method that cleans up the TextBox controls in the comment form on submit. That is the reason why Name and Email fields are not populated after posting the comment. So I can suggest you modify the custom control as follows:

    namespace MyControls 
        public class CommentsListExtender : CommentsList 
        { 
            public override string ProviderName 
            { 
                get 
                { 
                    if (string.IsNullOrEmpty(base.ProviderName)) 
                        return NewsManager.DefaultContentProvider; 
     
                    return base.ProviderName; 
                } 
                set 
                { 
                    base.ProviderName = value; 
                } 
            } 
     
            protected override void CreateChildControls() 
            { 
                object obj = this.Context.Items["NewsItem"]; 
                if (obj != null
                    this.articleId = (Guid)((UrlRewriteData)obj).Data; 
                if (this.articleId == Guid.Empty) 
                    return
     
                base.ParentID = articleId; 
                base.Moderation = false
     
                base.CreateChildControls(); 
     
                this.Container.SubmitButton.Command += new CommandEventHandler(SubmitButton_Command); 
                BuildComments(); 
            } 
     
            void SubmitButton_Command(object sender, CommandEventArgs e) 
            { 
                this.RecreateChildControls(); 
            } 
     
            void BuildComments() 
            { 
                if (!this.Page.User.Identity.IsAuthenticated) 
                { 
                    ((Button)this.Container.FindControl("submitBtn")).Enabled = false
                    this.Container.FindControl("lblNotLoggedIn").Visible = true
                } 
                else 
                { 
                    ((Button)this.Container.FindControl("submitBtn")).Enabled = true
                    this.Container.FindControl("lblNotLoggedIn").Visible = false
                    ((TextBox)this.Container.FindControl("authorTxt")).Text = Membership.GetUser().UserName.ToString(); 
                    ((TextBox)this.Container.FindControl("emailTxt")).Text = Membership.GetUser().Email.ToString(); 
                } 
            } 
     
            private Guid articleId; 
        } 

    Do let us know if this helps.

    All the best,
    Pepi
    the Telerik team

    Instantly find answers to your questions at the new Telerik Support Center
  7. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    25 Nov 2008
    Link to this post
    Ever since the update to 3.5, I am unable to get this code working again.  I recieve a Error message saying Object reference not set to an instance of an object.  It is highlight the base.CreateChildControls(); line of code in the CommentsListExtender class.
  8. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    26 Nov 2008
    Link to this post
    Hello Chris,

    I have tested the example in a project running v3.5 SP1 and it works fine. For your convenience I have attached the full code. Please test it and do let us know if you experience any problems.

    Greetings,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  9. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    26 Nov 2008
    Link to this post

    This is basically the same exact code I am using.  The only thing is it wont find any of the controls I have on the page using the

    ((Button)this.Container.FindControl("submitBtn")).Enabled = true;

    I do have all these controls in the template though.  I am able to access controls using the code.

    ((Button)this.Container.SubmitButton).Enabled = true

    This works for the fields that are already built into the class, but when I add an extra field like lblNotLoggedIn it wont let me access its attributes.

    Any Ideas?

  10. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    26 Nov 2008
    Link to this post
    The FindControl method should return a handle to your object at runtime (not give you intellisense at design time). Using a direct reference to an object within your container is only possible if the object is strongly typed in the backend, within the container class... and it still uses the FindControl method. If you want intellisense you will need to define the object in your container first ~ using the FindControl method should work though at runtime.
  11. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    26 Nov 2008
    Link to this post
    Thanks for the reply.  I am not worried about intellisense, the error is occuring at runtime.  It's not finding the control at runtime using the FindControl(control) method, even though the control is on my template. 
  12. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    26 Nov 2008
    Link to this post
    Perhaps you can post the error. A couple guesses, first make sure you are using the correct template, second make sure you are typing your object correctly. Also, make sure you are searching for the correct object name ~ it's a string so there is room for error. If you can post the source with your error it may help to resolve.
  13. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    26 Nov 2008
    Link to this post
    No problem, any help I can get is much appreciated.

    Here is the error:
     

    Server Error in '/OKCB' Application.

    Object reference not set to an instance of an object.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 74:             {
    Line 75:                 ((Button)this.Container.SubmitButton).Enabled = true;
    Line 76:                 this.Container.FindControl("lblNotLoggedIn").Visible = false;
    Line 77: 
    Line 78:                 ((TextBox)this.Container.AuthorText).Text = Membership.GetUser().UserName.ToString();

    Source File: c:\Program Files\telerik\Sitefinity3.5\WebSites\OKCB\App_Code\MyControls\CommentsListExtender.cs    Line: 76

    Stack Trace:

    [NullReferenceException: Object reference not set to an instance of an object.]
       OKCB.MyControls.CommentsListExtender.BuildComments() in c:\Program Files\telerik\Sitefinity3.5\WebSites\OKCB\App_Code\MyControls\CommentsListExtender.cs:76
       OKCB.MyControls.CommentsListExtender.CreateChildControls() in c:\Program Files\telerik\Sitefinity3.5\WebSites\OKCB\App_Code\MyControls\CommentsListExtender.cs:52
       System.Web.UI.Control.EnsureChildControls() +87
       System.Web.UI.Control.PreRenderRecursiveInternal() +50
       System.Web.UI.Control.PreRenderRecursiveInternal() +170
       System.Web.UI.Control.PreRenderRecursiveInternal() +170
       System.Web.UI.Control.PreRenderRecursiveInternal() +170
       System.Web.UI.Control.PreRenderRecursiveInternal() +170
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2041
    


    Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433


    And, here is the section of my commentslist template where I am using the label.

    <div> 
        <asp:Label ID="lblNotLoggedIn" runat="server" Text="Not Logged In"></asp:Label><br /> 
        <fieldset> 
                      
                    <asp:Label ID="Label1" runat="Server" Text="<%$Resources:Comment %>" AssociatedControlID="commentTxt"></asp:Label><br /> 
                    <asp:TextBox ID="commentTxt" runat="server" TextMode="MultiLine" Width="300px" ValidationGroup="commentInfo" Rows="6"></asp:TextBox> 
                    <asp:RequiredFieldValidator runat="server" ID="contentVal" ControlToValidate="commentTxt" ErrorMessage="<%$Resources:CommentEmpty %>" Display="dynamic" ValidationGroup="commentInfo" CssClass="commentsValidator"></asp:RequiredFieldValidator> 
     
                    <asp:TextBox ID="authorTxt" Visible="false" runat="server" ValidationGroup="commentInfo" size="30"></asp:TextBox> 
                    <asp:RequiredFieldValidator runat="server" ID="authorVal" ControlToValidate="authorTxt" ErrorMessage="<%$Resources:NameEmpty %>" Display="dynamic" ValidationGroup="commentInfo" CssClass="commentsValidator"></asp:RequiredFieldValidator>                     
     
                    <asp:TextBox ID="emailTxt" Visible="false" runat="server" ValidationGroup="commentInfo" size="30"></asp:TextBox> 
                    <asp:RegularExpressionValidator runat="server" ID="emailVal" ControlToValidate="emailTxt" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="<%$Resources:InvalidEmail %>" Display="dynamic" ValidationGroup="commentInfo" CssClass="commentsValidator"></asp:RegularExpressionValidator> 
     
                    <asp:Label ID="Label4" Visible="false" runat="Server" Text="<%$Resources:WebSite %>" AssociatedControlID="webSiteTxt"></asp:Label> 
                    <asp:TextBox ID="webSiteTxt" runat="server" size="30" Visible="false"></asp:TextBox> 
     
                    <asp:Button ID="submitBtn" runat="server" Text="<%$Resources:PostThisComment %>" ValidationGroup="commentInfo" CssClass="commentsButton" /> 
     
        </fieldset> 
        <br /><br /> 
    </div> 


    Thanks for any help,
    Chris Morrow
  14. Joel
    Joel avatar
    29 posts
    Registered:
    13 Feb 2008
    26 Nov 2008
    Link to this post
    A couple of quick thoughts ~ you are not typing your object (Label) so a generic object may not have a Visible property (although it does seem to appear that you are not getting a reference to an object at all). Secondly, I do not see where you are loading your template ~ without looking at the actual source and debugging locally I would think perhaps there is a new template being used and your old template path may not be the correct one any more. I would try removing your reference to the label in your code and seeing if the label is in fact rendered, regardless of login status. Seems to be a template related issue. You should also be able to drag and drop the default Sitefinity CommentsList control, go into edit and view the path to it's template, verify it is the correct path you are working against. Having upgrading to the latest version it seems possible this could be the issue.
  15. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    26 Nov 2008
    Link to this post
    I am defintely using the right template, I have been working with this template the whole time and all the other styles and layout changes that are made on the template are showing.  The label does show up on the page.(if I take the code out of the commentslistextender class so it doesn't throw an error).  It just doesn't seem to recognize the control at runtime.  I've also tried casting the object to a Label and experience the same result.

  16. Gabe Sumner
    Gabe Sumner avatar
    440 posts
    Registered:
    09 Sep 2007
    26 Nov 2008
    Link to this post

    Hey Chris,

    Is all this FindControl goodness happening after:

    base.CreateChildControls();
     
    I believe this is what instantiates the underlying ControlTemplate that Joel is referring to. 

    I'm coming late to this thread, so forgive me if this isn't useful.

    Gabe Sumner
    http://www.sitefinitywatch.com/

  17. Jackson
    Jackson avatar
    51 posts
    Registered:
    23 May 2008
    26 Nov 2008
    Link to this post

    Here is the class that I am using.  It should be calling base.CreateChildControls() before it ever reaches the BuildComments() method.

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using Telerik.Cms.Engine;  
    using Telerik.Cms.Engine.WebControls;  
    using Telerik.News;  
    using Telerik.Web;  
     
    /// <summary> 
    /// Summary description for CommentsListExtender  
    /// </summary> 
     
    namespace OKCB.MyControls  
    {  
        public class CommentsListExtender : CommentsList  
        {  
            public override string ProviderName  
            {  
                get  
                {  
                    if (string.IsNullOrEmpty(base.ProviderName))  
                        return NewsManager.DefaultContentProvider;  
     
                    return base.ProviderName;  
                }  
                set  
                {  
                    base.ProviderName = value;  
                }  
            }  
     
            protected override void CreateChildControls()  
            {  
                object obj = this.Context.Items["NewsItem"];  
                if (obj != null)  
                    this.articleId = (Guid)((UrlRewriteData)obj).Data;  
                if (this.articleId == Guid.Empty)  
                    return;  
     
                base.ParentID = articleId;  
                base.Moderation = false;  
     
                base.CreateChildControls();  
     
                this.Container.SubmitButton.Command += new CommandEventHandler(SubmitButton_Command);  
                BuildComments();  
            }  
     
            void SubmitButton_Command(object sender, CommandEventArgs e)  
            {  
                this.RecreateChildControls();  
                if (!this.Page.User.Identity.IsAuthenticated)  
                {  
                    this.ParentID = Guid.Empty;  
                    this.Page.Response.Redirect("~/login.aspx");  
                }    
     
            }  
     
            void BuildComments()  
            {  
                if (!this.Page.User.Identity.IsAuthenticated)  
                {  
                    ((Button)this.Container.SubmitButton).Enabled = true;  
                    Control lbl = this.Container.FindControl("lblNotLoggedIn");  
                    if (lbl != null)  
                        lbl.Visible = true;  
                }  
                else  
                {  
                    ((Button)this.Container.SubmitButton).Enabled = true;  
                    Control lbl = this.Container.FindControl("lblNotLoggedIn");  
                    if (lbl != null)  
                        lbl.Visible = false;  
                    ((TextBox)this.Container.AuthorText).Text = Membership.GetUser().UserName.ToString();  
                    ((TextBox)this.Container.EmailText).Text = Membership.GetUser().Email.ToString();  
                }  
            }  
     
            private Guid articleId;  
        }  
    }   
  18. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    27 Nov 2008
    Link to this post
    Hello Chris,

    Please open a new support ticket and send us the news template in which you use CommentsListExtender class.

    Thanks for your cooperation in advance.

    Kind regards,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
18 posts, 0 answered