Expose SpamProtector (Captcha) as a public property of a ContentView based control

March 01, 2010 Digital Experience

Each ContentView based control represents content items that have been created from the backend or programmatically. The ContentView based control has two modes

  • List - shows all items as a list.
  • Details - shows a single item from the list.

Build-in ContentView based controls are - EventsView, BlogPosts, NewsView, ContentView control, ImageGalley, DownloadList. The last two controls are ContentView based, but they have different presentation modes, because they are supposed to show data from Images and Documents module.

For each ContentView control you can enable comments. Enabling comments can be done through the control's Advanced/Properties tab where you will find Comments section. Once you have enabled the comments you may need to protect the form from online spammers. By default, SpamProtection is not enabled on Sitefinity comment. There is no public property that you can use to do this. Since 3.6 Sitefinity uses embedded templates, so first you need to map external template - ListPageDetails.ascx, then you need to find the SpamPropector declaration at the bottom of the template and enable SpamProtector control. You can save all these steps by creating a simple control that inherits from one of the ContentView based control and expose an user friendly public property. Then you only have to use this control instead of the built-in and set the custom property from the control designer.

1. Create a custom control that inherits from BlogPosts( or another ContentView based control). 

2. Override SingleItemTemplatePath and ItemListTemplatePath properties where you need to set the path to external templates that the control will use in List and Details mode.

3. Create a ViewState property of Boolean type (true/false) that you will use in the control designer. You can set Category and Description of this property, so that you can find it easily under Advanced/Properties tab. All properties that do not have Category set are added under Misc section.

4. Crete a control references to RadEditor and SpamPropector controls. You can find the controls from the control's SingleContainer.

5. Override CreateSingleContent method, set the RadEditor as visible and check the value of the custom property - ShowCaptcha.

public class BlogPostsCustomControl : BlogPosts
{
    public BlogPostsCustomControl()
    {
    }
     public override string  SingleItemTemplatePath
     {
     get
        {
            return "~/Sitefinity/Admin/ControlTemplates/Blogs/Modes/ListPageDetail.ascx";
        }
     
     }
 
    public override string  ItemListTemplatePath
    {
        get
        {
         return "~/Sitefinity/Admin/ControlTemplates/Blogs/Modes/ListPageMaster.ascx";
        }
    }
 
 
    public virtual bool ShowCaptcha
    {
        get
        {
            object obj = ViewState["ShowCaptcha"];
            if (obj != null)
                return (bool)obj;
            return true;
        }
        set
        {
            ViewState["ShowCaptcha"] = value;
        }
    }
  
 
    protected override void CreateSingleContent()
    {
        base.CreateSingleContent();
        Editor.Visible = true;
 
        if (ShowCaptcha)
        {
            Captcha.EnableCaptcha = true;
            Captcha.Visible = true;
        }
        else
        {
            Captcha.Visible = false;
        }
    }
 
 
    protected virtual SpamProtector Captcha
    {
 
        get
        {
 
            return ((Telerik.Web.UI.SpamProtection.SpamProtector)((Telerik.Blogs.WebControls.BlogCommentsList)(this.SingleContainer.CommentsListCtrl)).Controls[0].FindControl("spamProtector"));
 
        }
 
    }
 
    protected virtual RadEditor Editor
    {
        get
        {
           return ((Telerik.Blogs.WebControls.BlogCommentsList)(this.SingleContainer.CommentsListCtrl)).Controls[0].FindControl("commentTxt") as RadEditor;
        }
    }
 
}

The Progress Team