How to Use the BotDetect CAPTCHA Feature in SharePoint 2007 WebParts

Using BotDetect CAPTCHA feature in SharePoint 2007 Web Part

This article describes steps for creating a SharePoint Web Part using the BotDetect ASP.NET CAPTCHA control. It is a very simple Web Part that allows you to validate user input, comparing it to the code shown in the CAPTCHA image.


  • Microsoft Visual Studio 2008 or 2010 (or newer)
  • BotDetect ASP.NET CAPTCHA vMicrosoft Visual Studio 2008 or 2010 (or newer)

In this article we assume that you are running Visual Studio 2008 or 2010 (or newer) on a server running WSS (Windows SharePoint Services) or MOSS (Microsoft Office SharePoint Server) 2007.
Start by downloading the sample SharePoint CAPTCHA Web Part project. If you want to use this simplified Web Part example in your SharePoint website directly, you just have to build the project and follow steps 9-12.
Steps 1-8 describe how the example Web Part project was created, and you can follow them if you want to create your own CAPTCHA Web Part — and make any code changes your SharePoint projects might require.

Step 1: Creating a new Web Part Library project

In Visual Studio:

  1. On the File menu, click New Project
  2. In the New Project dialog box, click Visual C#, then Web, and finally select the ASP.NET Server Control project type
  3. Type "BotDetectWebPart" as the name and specify the location for the project files, and then click OK.

To create a Web Part, you need to add a reference to the Microsoft.SharePoint, System.XML and BotDetect (.NET 3.5) assemblies.
If you want to reference the BotDetect Captcha control from an anonymous SharePoint site's Bin folder, the IIS anonymous access user account (IUSR_<SERVER_NAME>) should be granted Readpermissions to Lanap.BotDetect.dll. Alternatively, you could reference the BotDetect.dll assembly from the GAC.

Step 2: Initial Project Settings

Before you start working with the code for a Web Part, you must make the following changes to your project settings:

  • Rename ServerControl1.cs to BotDetectWebPart.cs
  • Set the version number
  • Strong-name the assembly

To set the version number:

  1. In Solution Explorer, double-click AssemblyInfo.cs
  2. Edit the line:
    [assembly: AssemblyVersion("1.0.*")]
    so that it reads:
    [assembly: AssemblyVersion ("")]

To strong-name the assembly:

  1. Right click on your project name and select properties
  2. Choose Signing tab
  3. Check "Sign the assembly" option
  4. In "Choose a strong name key file" choose "<New...>"
  5. In new dialog type a file name and optionally a password
  6. Save project settings

Step 3: Adding namespace directives

  • Add the following using directives near the top of your code:
  • using Microsoft.SharePoint.WebPartPages;
    using System.Xml.Serialization;
    using System.Web.UI.HtmlControls;
    using BotDetect;
  • using BotDetect.Web.UI;

Step 4: Defining Toolbox data

Replace the following line:
[ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
with this line:
[ToolboxData("<{0}:BotDetectSimpleWebPart runat=server>")]

Step 5: Inheriting from the WebPart base class

Replace this line of code:
public class WebCustomControl1 : WebControl
public class BotDetectSimpleWebPart : WebPart

Step 6: Defining a XML namespace for the entire WebPart class

Add this line of code above the declaration for your WebPart class:

Step 7: Defining the Logic and Rendering of Your Web Part

After you complete the previous steps, you can define the logic and rendering for your Web Part.
We are going to add a BotDetect.Captcha object (displaying the CAPTCHA image), a TextBox (entering the CAPTCHA code), a Button (invoking CAPTCHA validation), and a Label (displaying the CAPTCHA validation result).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
using BotDetect;
using BotDetect.Web.UI;
using Microsoft.SharePoint.WebPartPages;
namespace BotDetectCaptchaWebPart2007
  [ToolboxData("<{0}:BotDetectSimpleWebPart runat=server>")]
  public class BotDetectSimpleWebPart : WebPart
    private TextBox _inputText;
    private Button _submitButton;
    private Label _messageLabel;
    private WebFormsCaptcha _captchaControl;
    protected override void CreateChildControls()
      _captchaControl = new WebFormsCaptcha();
      _captchaControl.ID = "BotDetectCaptcha";
      _inputText = new TextBox();
      _inputText.ID = "InputText";
      _captchaControl.UserInputID = "_inputText.ClientID;
      _submitButton = new Button();
      _submitButton.ID = "SubmitButton";
      _submitButton.Text = "Validate";
      _submitButton.Click +=
        new EventHandler(submitButton_Click);
      _messageLabel = new Label();
      _messageLabel.ID = "MessageLabel";
    void submitButton_Click(object sender, EventArgs e)
      string captchaText =
      ((TextBox)Controls[1]).Text = null;
      if (_captchaControl.Validate(captchaText))
        ((Label)Controls[3]).Text = "Success!";
        ((Label)Controls[3]).Text = "Error!";

Step 8: Creating a Web Part Definition File (.dwp)

A Web Part Definition file (.dwp) file is a simple XML file that contains property settings for a single Web Part. To import your Web Part into a Web Part Page, simply upload the .dwp file. After uploading the Web Part, you can display the Web Part by dragging it into one of the zones of the Web Part Page.
Two properties are required in the .dwp file: Assembly and TypeName. However, to display a default name and description for the Web Part after it is imported, you should also include the Title and Description properties. If you want to set other Web Part properties during import, you can also define them in a .dwp file. A .dwp file takes the following form:

<?xml version="1.0"?>
<WebPart xmlns="">
   <Assembly>AssemblyName(with no .dll extension),
      Version=VersionNumber, Culture=Culture,

Your Web Part's .dwp part should look like this one:

<?xml version="1.0"?>
<WebPart xmlns="">
  <Assembly>BotDetectCaptchaWebPart2007, Version=, Culture=Neutral,
  <Title>Bot Detect Simple Web Part</Title>
  <Description>This web part shows BotDetect
    Captcha image and validates user input</Description>

Step 9: Register a Web Part assembly as a SafeControl

  1. Open the web.config file of your SharePoint site.
  2. Add the following lines in the <SafeControls> block:
        Assembly="BotDetectCaptchaWebPart2007, Version=, Culture=neutral,
  3. Replace the PublicKeyToken value (5ea8ae17e5cc573f) with the actual value for your Web Part's assembly. To determine the correct PublicKeyToken value for the Assembly attribute of the <SafeControl> tag of your Web Part, use the sn.exe command-line utility:
    sn.exe -T C:\inetpub\wwwroot\bin\BotDetectCaptchaWebPart2007.dll

Step 10: Registering Http Handlers & Modules

  • Add the following lines to the <system.web> section of the SharePoint "Web.config" file:
    <add verb="GET" path="BotDetectCaptcha.ashx" 
      type="BotDetect.Web.CaptchaHandler, BotDetect, Version=, 
      Culture=neutral, PublicKeyToken=74616036388b765f" />
  • Make sure ASP.NET Session State is allowed in the <httpModules> section of the SharePoint web.config file - if the following element is missing or commented-out, please add it:
      <add name="Session" type="System.Web.SessionState.SessionStateModule" />
  • Make sure ASP.NET Session State is allowed in the <pages> section of the SharePointweb.config file - the enableSessionState attribute value must be set to true:
    <pages enableSessionState="true" enableViewState="true" 
      enableViewStateMac="true" validateRequest="false" 
        SPPageParserFilter, Microsoft.SharePoint, Version=, 
        Culture=neutral, PublicKeyToken=71e9bce111e9429c" 

You can download a example web.config file here.

Step 11: Deploying Your Web Part

To deploy your Web Part to a SharePoint site, copy your web part's assembly to the global assembly cache folder at C:\windows\assembly.

Step 12: Importing Your Web Part into a Web Part Page

To use and test your Web Part, import it into a Web Part Page on a server running WSS (Windows SharePoint Services) or MOSS (Microsoft Office SharePoint Server) 2007.

  1. Open a Web Part Page on your server.
  2. Select Edit Page from the Site Actions menu in the upper right corner of the page, and click Add a Web Part in one of the zones.
  3. In the Add Web Parts dialog box, click Advanced Web Part gallery and options in the lower right corner.
  4. In the Add Web Parts pane, click Import.
  5. Browse to the location of the BotDetectSimpleWebPart.dwp file, and then click the Upload button. After uploading, the page will refresh, and "My Simple Web Part" should be displayed under Imported Web Part.
  6. Drag the icon next to "Bot Detect Simple Web Part" to a zone on the Web Part Page.
  7. Type some text in the text box, and then click Set Web Part Title to test the part.