How To create a SharePoint Web Part with BotDetect CAPTCHA (BotDetect v2.0; deprecated)
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.
Prerequisites
- Microsoft Visual Studio 2005 or 2008
- Microsoft Windows SharePoint Services 2007
- BotDetect ASP.NET CAPTCHA v2.0.11 or newer — if you don't have it already, you can download the free version version
In this article we assume that you are running Visual Studio 2005 or 2008 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 sample in your SharePoint website directly, you just have to build the project and follow steps 9-12.
Steps 1-8 describe how the sample 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
If you are using Visual Studio 2008:
- On the File menu, click New Project
- In the New Project dialog box, click Visual C#, then Web, and finally select the ASP.NET Server Control project type
- Type "LanapWebPart" as the name and specify the location for the project files, and then click OK
If you are using Visual Studio 2005:
- On the File menu, point to New, and then click Project
- In the New Project dialog box, click Visual C# Projects, and then select the Web Control Library template
- Type "LanapWebPart" 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 Lanap.BotDetect 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 Read permissions to Lanap.BotDetect.dll. Alternatively, you could reference the Lanap.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:
- Set the version number
- Strong-name the assembly
To set the version number:
- In Solution Explorer, double-click AssemblyInfo.cs.
- Edit the line:
[assembly: AssemblyVersion("1.0.*")]so that it reads:[assembly: AssemblyVersion ("1.0.0.0")]
To strong-name the assembly:
- Right click on your project name and select properties
- Choose Signing tab
- Check "Sign the assembly" option
- In "Choose a strong name key file" choose "<New...>"
- In new dialog type a file name and optionally a password
- 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 Lanap.BotDetect;
Step 4: Defining Toolbox data
- Replace the following line:
[ToolboxData("<{0}:WebCustomControl1 runat=server>{0}:WebCustomControl1>")]with this line:[ToolboxData("<{0}:LanapSimpleWebPart runat=server>{0}:SimpleWebPart>")]
Step 5: Inheriting from the WebPart base class
- Replace this line of code:
public class WebCustomControl1 : WebControl
withpublic class LanapSimpleWebPart : WebPart
Step 6: Defining a XML namespace for the entire WebPart class
- Add this line of code above the declaration for your WebPart class:
[XmlRoot(Namespace="LanapWebPart")]
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 Lanap.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 Microsoft.SharePoint.WebPartPages; using System.Xml.Serialization; using System.Web.UI.HtmlControls; using Lanap.BotDetect; namespace LanapWebPart { [XmlRoot(Namespace = "LanapWebPart")] [ToolboxData("<{0}:LanapSimpleWebPart runat=server>{0}:SimpleWebPart>")] public class LanapSimpleWebPart : WebPart { private TextBox _inputText; private Button _submitButton; private Label _messageLabel; private Lanap.BotDetect.Captcha _captchaControl; protected override void CreateChildControls() { base.CreateChildControls(); _captchaControl = new Lanap.BotDetect.Captcha(); _captchaControl.ID = "LanapCaptcha"; _inputText = new TextBox(); _inputText.ID = "InputText"; _submitButton = new Button(); _submitButton.ID = "SubmitButton"; _submitButton.Text = "Validate"; _submitButton.Click += new EventHandler(submitButton_Click); _messageLabel = new Label(); _messageLabel.ID = "MessageLabel"; this.Controls.Add(_captchaControl); this.Controls.Add(_inputText); this.Controls.Add(_submitButton); this.Controls.Add(_messageLabel); } void submitButton_Click(object sender, EventArgs e) { string captchaText = ((TextBox)Controls[1]).Text.Trim().ToUpper(); ((TextBox)Controls[1]).Text = null; if (_captchaControl.Validate(captchaText)) { ((Label)Controls[3]).Text = "Success!"; } else { ((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="http://schemas.microsoft.com/WebPart/v2"> <Assembly>AssemblyName(with no .dll extension), Version=VersionNumber, Culture=Culture, PublicKeyToken=PublicKeyToken</Assembly> <TypeName>WebPartNamespace.WebPartClassName</TypeName> <Title>DefaultWebPartTitle</Title> <Description>WebPartDescription</Description> </WebPart>Your Web Part's .dwp part should look like this one:
<?xml version="1.0"?> <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2"> <Assembly>LanapWebPart, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=def148956c61a16b</Assembly> <TypeName>LanapWebPart.LanapSimpleWebPart</TypeName> <Title>Lanap Simple Web Part</Title> <Description>This web part shows Lanapsoft BotDetect Captcha image and validates user input</Description> </WebPart>
Step 9: Register a Web Part assembly as a SafeControl
- Open the web.config file of your SharePoint site.
- Add the following lines in the <SafeControls> block:
<SafeControl Assembly="LanapWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def148956c61a16b Namespace="LanapWebPart" TypeName="*" Safe="True" /> - Replace the PublicKeyToken value (def148956c61a16b) 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\LanapWebPart.dll
Step 10: Registering Http Handlers & Modules
- Add the following lines to the <system.web> section of the SharePoint "Web.config" file:
<httpHandlers> <add verb="*" path="LanapCaptcha.ashx" type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect" /> </httpHandlers> Or, if you want to load the Lanap.BotDetect.dll assembly from the GAC, modify the httpHandlers declaration to:
<add verb="*" path="LanapCaptcha.ashx" type="Lanap.BotDetect.CaptchaHandler, Lanap.BotDetect, Version=2.0.15.0, Culture=neutral, PublicKeyToken=74616036388b765f"/>- Add the following lines to the <appSettings> section of the SharePoint "Web.config" file:
<appSettings> <add key="LBD_RequestPath" value="LanapCaptcha.ashx" /> </appSettings>
- Modify the <sessionState> element declaration to include the sessionIDManagerType attribute with the following value:
<sessionState mode="InProc" cookieless="AutoDetect" timeout="20" sessionIDManagerType=" Lanap.BotDetect.Persistence.CustomSessionIDManager, Lanap.BotDetect" />Or, if you want to load the Lanap.BotDetect.dll assembly from the GAC, use:
<sessionState mode="InProc" cookieless="AutoDetect" timeout="20" sessionIDManagerType=" Lanap.BotDetect.Persistence.CustomSessionIDManager, Lanap.BotDetect, Version=2.0.15.0, Culture=neutral, PublicKeyToken=74616036388b765f" />This is a workaround required for the audio CAPTCHA to work properly for IE 7.0 Vista and Google Chrome users, as explained in the BotDetect ASP.NET CAPTCHA FAQ. - 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:
<httpModules> <add name="Session" type="System.Web.SessionState.SessionStateModule" /> </httpModules> - Make sure ASP.NET Session State is allowed in the <pages> section of the SharePoint web.config file - the enableSessionState attribute value must be set to true:
<pages enableSessionState="true" enableViewState="true" enableViewStateMac="true" validateRequest="false" pageParserFilterType="Microsoft.SharePoint.ApplicationRuntime. SPPageParserFilter, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" asyncTimeout="7">
Step 11: Deploying Your Web Part
- To deploy your Web Part to a SharePoint site, copy your web part's assembly to the C:\inetpub\wwwroot\bin folder of your SharePoint server (or 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.
- Open a Web Part Page on your server.
- 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.
- In the Add Web Parts dialog box, click Advanced Web Part gallery and options in the lower right corner.
- In the Add Web Parts pane, click Import.
- Browse to the location of the SimpleWebPart.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.
- Drag the icon next to "My Simple Web Part" to a zone on the Web Part Page.
- Type some text in the text box, and then click Set Web Part Title to test the part.
Please Note
The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v2.0).
An up-to-date equivalent page for the latest BotDetect Captcha release (v3) is BotDetect v3 Captcha documentation index.
General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v3.0 page.
Please Note
The information on this page is out of date and applies to a deprecated version of BotDetect™ CAPTCHA (v2.0).
An up-to-date equivalent page for the latest BotDetect Captcha release (v3) is BotDetect v3 Captcha documentation index.
General information about the major improvements in the current BotDetect release can be found at the What's New in BotDetect v3.0 page.
Current BotDetect Versions
- BotDetect PHP CAPTCHA v3.0.Beta12013 May 20
- BotDetect ASP.NET CAPTCHA v3.0.142013 May 20
- BotDetect ASP Classic CAPTCHA v3.0.142013 May 20




