Create a control template ascx file in the 12\TEMPLATE\CONTROLTEMPLATES folder containing:
<%@ Control Language="C#" %>
<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<SharePoint:RenderingTemplate ID="txtFieldRendering" runat="server">
<Template>
<asp:TextBox ID="RegExText" CssClass="ms-long" runat="server"></asp:TextBox><br />
</Template>
</SharePoint:RenderingTemplate>
Create a field definition file in the 12\TEMPLATE\XML folder containing:
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">RegExField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">RegEx Text Field</Field>
<Field Name="TypeShortDescription">Text field that is validated by a regular expression</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">[YOUR ASSEMBLY REFERENCE HERE]</Field>
<PropertySchema>
<Fields>
<Field Name="RegularExpression" DisplayName="Regular Expression" MaxLength="1024" DisplaySize="50" Type="Note">
<Default>w+([-+.']w+)*@w+([-.]w+)*.w+([-.]w+)*</Default>
</Field>
<Field Name="ErrorMessage" DisplayName="Error Message" MaxLength="255" DisplaySize="50" Type="Text">
<Default>Please enter a valid email address</Default>
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr>
<Column />
</Expr>
<Case Value="" />
<Default>
<Column SubColumnNumber="0" HTMLEncode="True" />
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>
And create two classes in your source code for your assembly:
using System;
using System.Web;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace RegExField
{
public class RegExControlField : SPFieldText
{
public RegExControlField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public RegExControlField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
public override string GetValidatedString(object value)
{
String txtToValidate = value.ToString();
if (Required || txtToValidate.Trim() != "")
{
Regex exp = new Regex(GetCustomProperty("RegularExpression").ToString());
String err = GetCustomProperty("ErrorMessage").ToString();
if (!exp.IsMatch(txtToValidate))
throw new SPFieldValidationException(err);
}
return base.GetValidatedString(value);
}
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new RegExControlFieldControl();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
}
}
And:
using System;Compile the classes above into an strong named assembly, install into the GAC, amend the reference in the field definition (the xml file), and an iisreset. The field should then be available to choose when creating a new column in a list.
using System.Web;
using System.Web.UI;
using System.Runtime.InteropServices;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace RegExField
{
public class RegExControlFieldControl : BaseFieldControl
{
protected System.Web.UI.WebControls.TextBox txtRegExText;
protected override string DefaultTemplateName
{
get
{
return "txtFieldRendering";
}
}
public override object Value
{
get
{
EnsureChildControls();
return this.txtRegExText.Text;
}
set
{
EnsureChildControls();
this.txtRegExText.Text = this.ItemFieldValue.ToString();
}
}
protected override void CreateChildControls()
{
if (Field == null || ControlMode == SPControlMode.Display)
return;
base.CreateChildControls();
this.txtRegExText = (System.Web.UI.WebControls.TextBox)TemplateContainer.FindControl("RegExText");
}
}
}
The next post will be concerned with wrapping this field definition into a deployable Sharepoint Solution Package (wsp).

No comments:
Post a Comment