1. 首先创建一个Field 项目CustomFields.
2. 添加三个类文件:FieldAddressValue.cs ,FieldAddress.cs,AddressFieldControl.cs三个文件.
1) FieldAddressValue.cs源代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web;
namespace CustomFields
{
class FieldAddressValue : SPFieldMultiColumnValue
{
private const int numberOfFields = 4;
public FieldAddressValue() : base(numberOfFields) { }
public FieldAddressValue(string value) : base(value) { }
public string Address
{
get { return this[0]; }
set { this[0] = value; }
}
public string Zip
{
get { return this[1]; }
set { this[1] = value; }
}
public string City
{
get { return this[2]; }
set { this[2] = value; }
}
public string Country
{
get { return this[3]; }
set { this[3] = value; }
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.Web;
namespace CustomFields
{
class FieldAddressValue : SPFieldMultiColumnValue
{
private const int numberOfFields = 4;
public FieldAddressValue() : base(numberOfFields) { }
public FieldAddressValue(string value) : base(value) { }
public string Address
{
get { return this[0]; }
set { this[0] = value; }
}
public string Zip
{
get { return this[1]; }
set { this[1] = value; }
}
public string City
{
get { return this[2]; }
set { this[2] = value; }
}
public string Country
{
get { return this[3]; }
set { this[3] = value; }
}
}
}
2) FieldAddress.cs源代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using CustomFields.WebControls;
namespace CustomFields
{
class FieldAddress : SPFieldMultiColumn
{
public FieldAddress(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
public FieldAddress(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fldControl = new AddressFieldControl();
fldControl.FieldName = InternalName;
return fldControl;
}
}
public override object GetFieldValue(string value)
{
if(string .IsNullOrEmpty(value))
return null;
return new FieldAddressValue(value);
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using CustomFields.WebControls;
namespace CustomFields
{
class FieldAddress : SPFieldMultiColumn
{
public FieldAddress(SPFieldCollection fields, string fieldName) : base(fields, fieldName) { }
public FieldAddress(SPFieldCollection fields, string typeName, string displayName) : base(fields, typeName, displayName) { }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fldControl = new AddressFieldControl();
fldControl.FieldName = InternalName;
return fldControl;
}
}
public override object GetFieldValue(string value)
{
if(string .IsNullOrEmpty(value))
return null;
return new FieldAddressValue(value);
}
}
}
3) AddressFieldControl.cs源代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI.WebControls;
namespace CustomFields.WebControls
{
class AddressFieldControl : BaseFieldControl
{
protected TextBox addressBox;
protected TextBox zipBox;
protected TextBox cityBox;
protected TextBox countryBox;
protected override string DefaultTemplateName
{
get
{
return "AddressFieldRendering";
}
}
public override object Value
{
get
{
EnsureChildControls();
FieldAddressValue fieldValue = new FieldAddressValue();
fieldValue.Address = addressBox.Text.Trim();
fieldValue.Zip = zipBox.Text.Trim();
fieldValue.City = cityBox.Text.Trim();
fieldValue.Country = countryBox.Text.Trim();
return fieldValue;
}
set
{
EnsureChildControls();
FieldAddressValue fieldValue = (FieldAddressValue)value;
addressBox.Text = fieldValue.Address;
zipBox.Text = fieldValue.Zip;
cityBox.Text = fieldValue.City;
countryBox.Text = fieldValue.Country;
}
}
public override void Focus()
{
EnsureChildControls();
addressBox.Focus();
}
protected override void CreateChildControls()
{
if (Field == null)
return;
base.CreateChildControls();
if (ControlMode == SPControlMode.Display)
return;
addressBox = (TextBox)TemplateContainer.FindControl("addressBox");
if (addressBox == null)
throw new ArgumentException("Corrupted AddressFieldRendering template - missing addressBox. ");
addressBox.TabIndex = TabIndex;
addressBox.CssClass = CssClass;
addressBox.ToolTip = Field.Title + " Address";
zipBox = (TextBox)TemplateContainer.FindControl("zipBox");
if (zipBox == null)
throw new ArgumentException("corrupted AddressFieldRendering template - missing zipBox.");
zipBox.TabIndex = TabIndex;
zipBox.CssClass = CssClass;
zipBox.ToolTip = Field.Title + " Zipcode";
cityBox = (TextBox)TemplateContainer.FindControl("cityBox");
if (cityBox == null)
throw new ArgumentException("corrupted AddressFieldRendering template - missing cityBox.");
cityBox.TabIndex = TabIndex;
cityBox.CssClass = CssClass;
cityBox.ToolTip = Field.Title + " City";
countryBox = (TextBox)TemplateContainer.FindControl("countryBox");
if (countryBox == null)
throw new ArgumentException("corrupted AddressFieldRendering template - missing countryBox.");
countryBox.TabIndex = TabIndex;
countryBox.CssClass = CssClass;
countryBox.ToolTip = Field.Title + " Country";
if (ControlMode == SPControlMode.New)
{
addressBox.Text = Field.GetCustomProperty("DefaultStreet").ToString();
zipBox.Text = Field.GetCustomProperty("DefaultZip").ToString();
cityBox.Text = Field.GetCustomProperty("DefaultCity").ToString();
countryBox.Text = Field.GetCustomProperty("DefaultCountry").ToString();
}
}
}
}
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI.WebControls;
namespace CustomFields.WebControls
{
class AddressFieldControl : BaseFieldControl
{
protected TextBox addressBox;
protected TextBox zipBox;
protected TextBox cityBox;
protected TextBox countryBox;
protected override string DefaultTemplateName
{
get
{
return "AddressFieldRendering";
}
}
public override object Value
{
get
{
EnsureChildControls();
FieldAddressValue fieldValue = new FieldAddressValue();
fieldValue.Address = addressBox.Text.Trim();
fieldValue.Zip = zipBox.Text.Trim();
fieldValue.City = cityBox.Text.Trim();
fieldValue.Country = countryBox.Text.Trim();
return fieldValue;
}
set
{
EnsureChildControls();
FieldAddressValue fieldValue = (FieldAddressValue)value;
addressBox.Text = fieldValue.Address;
zipBox.Text = fieldValue.Zip;
cityBox.Text = fieldValue.City;
countryBox.Text = fieldValue.Country;
}
}
public override void Focus()
{
EnsureChildControls();
addressBox.Focus();
}
protected override void CreateChildControls()
{
if (Field == null)
return;
base.CreateChildControls();
if (ControlMode == SPControlMode.Display)
return;
addressBox = (TextBox)TemplateContainer.FindControl("addressBox");
if (addressBox == null)
throw new ArgumentException("Corrupted AddressFieldRendering template - missing addressBox. ");
addressBox.TabIndex = TabIndex;
addressBox.CssClass = CssClass;
addressBox.ToolTip = Field.Title + " Address";
zipBox = (TextBox)TemplateContainer.FindControl("zipBox");
if (zipBox == null)
throw new ArgumentException("corrupted AddressFieldRendering template - missing zipBox.");
zipBox.TabIndex = TabIndex;
zipBox.CssClass = CssClass;
zipBox.ToolTip = Field.Title + " Zipcode";
cityBox = (TextBox)TemplateContainer.FindControl("cityBox");
if (cityBox == null)
throw new ArgumentException("corrupted AddressFieldRendering template - missing cityBox.");
cityBox.TabIndex = TabIndex;
cityBox.CssClass = CssClass;
cityBox.ToolTip = Field.Title + " City";
countryBox = (TextBox)TemplateContainer.FindControl("countryBox");
if (countryBox == null)
throw new ArgumentException("corrupted AddressFieldRendering template - missing countryBox.");
countryBox.TabIndex = TabIndex;
countryBox.CssClass = CssClass;
countryBox.ToolTip = Field.Title + " Country";
if (ControlMode == SPControlMode.New)
{
addressBox.Text = Field.GetCustomProperty("DefaultStreet").ToString();
zipBox.Text = Field.GetCustomProperty("DefaultZip").ToString();
cityBox.Text = Field.GetCustomProperty("DefaultCity").ToString();
countryBox.Text = Field.GetCustomProperty("DefaultCountry").ToString();
}
}
}
}
3. 强名称编译程序集.
4. 添加一个类型定义文件fldtypes_custom.xml.
fldtypes_custom.xml文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">Address</Field>
<Field Name="ParentType">MultiColumn</Field>
<Field Name="TypeDisplayName">Address</Field>
<Field Name="TypeShortDescription">Address (street + nr, zip, city and country)</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListCreate">TRUE</Field>
<Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
<Field Name="ShowOnSurveyCreate">TRUE</Field>
<Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
<Field Name="FieldTypeClass">CustomFields.FieldAddress,CustomFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=55a361e8b234515f</Field>
<PropertySchema>
<Fields>
<Field Name="DefaultStreet" DisplayName="Default Street:" MaxLength="50" DisplaySize="30" Type="Text">
<Default>XiaoYuan Road</Default>
</Field>
<Field Name="DefaultZip" DisplayName="Default Zip:" MaxLength="10" DisplaySize="10" Type="Text">
<Default>100027</Default>
</Field>
<Field Name="DefaultCity" DisplayName="Default City:" MaxLength="50" DisplaySize="30" Type="Text">
<Default>Beijing</Default>
</Field>
<Field Name="DefaultCountry" DisplayName="Default Country:" MaxLength="50" DisplaySize="30" Type="Text">
<Default>China</Default>
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr><Column/></Expr>
<Case Value="">
</Case>
<Default>
<Column SubColumnNumber="0" HTMLEncode="TRUE" />
<HTML><![CDATA[<BR>]]></HTML>
<Column SubColumnNumber="1" HTMLEncode="TRUE"/>
<HTML><![CDATA[ ---- ]]></HTML>
<Column SubColumnNumber="2" HTMLEncode="TRUE"/>
<HTML><![CDATA[<BR>]]></HTML>
<Column SubColumnNumber="3" HTMLEncode="TRUE"/>
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>
<FieldTypes>
<FieldType>
<Field Name="TypeName">Address</Field>
<Field Name="ParentType">MultiColumn</Field>
<Field Name="TypeDisplayName">Address</Field>
<Field Name="TypeShortDescription">Address (street + nr, zip, city and country)</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListCreate">TRUE</Field>
<Field Name="ShowOnDocumentLibraryCreate">TRUE</Field>
<Field Name="ShowOnSurveyCreate">TRUE</Field>
<Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
<Field Name="FieldTypeClass">CustomFields.FieldAddress,CustomFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=55a361e8b234515f</Field>
<PropertySchema>
<Fields>
<Field Name="DefaultStreet" DisplayName="Default Street:" MaxLength="50" DisplaySize="30" Type="Text">
<Default>XiaoYuan Road</Default>
</Field>
<Field Name="DefaultZip" DisplayName="Default Zip:" MaxLength="10" DisplaySize="10" Type="Text">
<Default>100027</Default>
</Field>
<Field Name="DefaultCity" DisplayName="Default City:" MaxLength="50" DisplaySize="30" Type="Text">
<Default>Beijing</Default>
</Field>
<Field Name="DefaultCountry" DisplayName="Default Country:" MaxLength="50" DisplaySize="30" Type="Text">
<Default>China</Default>
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<Switch>
<Expr><Column/></Expr>
<Case Value="">
</Case>
<Default>
<Column SubColumnNumber="0" HTMLEncode="TRUE" />
<HTML><![CDATA[<BR>]]></HTML>
<Column SubColumnNumber="1" HTMLEncode="TRUE"/>
<HTML><![CDATA[ ---- ]]></HTML>
<Column SubColumnNumber="2" HTMLEncode="TRUE"/>
<HTML><![CDATA[<BR>]]></HTML>
<Column SubColumnNumber="3" HTMLEncode="TRUE"/>
</Default>
</Switch>
</RenderPattern>
</FieldType>
</FieldTypes>
5. 添加一个模板文件AddressFieldRendering.ascx.
AddressFieldRendering.ascx文件内容如下:
<%@ Control Language="C#" Debug="true"%>
<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
<SharePoint:RenderingTemplate ID="AddressFieldRendering" runat="server">
<Template>
<table class="ms-form">
<tr>
<td>Address:</td>
<td><asp:TextBox ID="addressBox" MaxLength="255" size="50" runat="server" /></td>
</tr>
<tr>
<td>Zip:</td>
<td><asp:TextBox ID="zipBox" MaxLength="10" size="5" runat="server" /></td>
</tr>
<tr>
<td>City:</td>
<td><asp:TextBox ID="cityBox" MaxLength="50" size="15" runat="server" /></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:TextBox ID="countryBox" MaxLength="50" size="15" runat="server" /></td>
</tr>
</table>
</Template>
</SharePoint:RenderingTemplate>
<%@ Assembly Name="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
<SharePoint:RenderingTemplate ID="AddressFieldRendering" runat="server">
<Template>
<table class="ms-form">
<tr>
<td>Address:</td>
<td><asp:TextBox ID="addressBox" MaxLength="255" size="50" runat="server" /></td>
</tr>
<tr>
<td>Zip:</td>
<td><asp:TextBox ID="zipBox" MaxLength="10" size="5" runat="server" /></td>
</tr>
<tr>
<td>City:</td>
<td><asp:TextBox ID="cityBox" MaxLength="50" size="15" runat="server" /></td>
</tr>
<tr>
<td>Country:</td>
<td><asp:TextBox ID="countryBox" MaxLength="50" size="15" runat="server" /></td>
</tr>
</table>
</Template>
</SharePoint:RenderingTemplate>
6. 部署.
1) 把 程序集CustomFields加入GAC.
2) 把类型定义文件fldtypes_custom.xml拷贝到目录c:\program...\12\TEMPLATE\XML下.
3) 把模板文件AddressFieldRendering.ascx拷贝到目录c:\program...\12\TEMPLATE\CONTROLTEMPLATES下.
4) 重起IIS.