这个实例主要是建义一书中的开发实例!字段类型由3部分组成:继承于SPField的字段类,继承于BaseRenderControl的字段呈现控件类和字段的的配置文件。
下面是这个项目的结构:
EmailFieldControls.cs
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FieldControlsDevelop
{
public class EmailFieldControl:BaseFieldControl
{
private TextBox _textbox;
protected override void CreateChildControls()
{
base.CreateChildControls();
//show
if (this.ControlMode == SPControlMode.Display)
{
this.Controls.Add(new LiteralControl("" + this.Value));
}
else
{
//new and edit mode
_textbox = new TextBox();
this.Controls.Add(_textbox);
}
}
public override object Value
{
get
{
this.EnsureChildControls();
if (this._textbox != null)
return this._textbox.Text;
else
return null;
}
set
{
this.EnsureChildControls();
if (this._textbox != null)
{
this._textbox.Text = "" + value;
}
}
}
}
}
EmailField.cs
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text.RegularExpressions;
namespace FieldControlsDevelop
{
public class EmailField:SPFieldText
{
public EmailField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{
}
public EmailField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{
}
/// <summary>
/// return the show control
/// </summary>
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl ctl = new EmailFieldControl();
ctl.FieldName = this.InternalName;
return ctl;
}
}
public override string GetValidatedString(object value)
{
if (Required &&(value==null||value.ToString()==""))
throw new SPFieldValidationException(SPResource.GetString(Strings.MissingRequiredField));
if (value.ToString() == "")
return value.ToString();
//Match the email format
string emailRE = @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
if (!Regex.IsMatch(value.ToString(), emailRE))
throw new SPFieldValidationException("The email format is not correct!.");
if (value == null)
return string.Empty;
else
return value.ToString();
}
#region Not Use
//private static readonly Dictionary<string, StringDictionary> CustomPropertiesCache = new Dictionary<string, StringDictionary>();
///// <summary>
///// get the unque key
///// </summary>
//private string ContextKey
//{
// get
// {
// return this.ParentList.ID.ToString() + "_" + HttpContext.Current.GetHashCode();
// }
//}
///// <summary>
///// set the custom property from cache
///// </summary>
///// <param name="key"></param>
///// <param name="value"></param>
//protected void SetCustomPropertytoCache(string key, string value)
//{
// StringDictionary plist = null;
// if (CustomPropertiesCache.ContainsKey(ContextKey))
// {
// plist = CustomPropertiesCache[ContextKey];
// }
// else
// {
// plist = new StringDictionary();
// CustomPropertiesCache.Add(ContextKey, plist);
// }
// if (plist.ContainsKey(key))
// plist[key] = value;
// else
// plist.Add(key, value);
//}
///// <summary>
///// get the custom property from cache
///// </summary>
///// <param name="key"></param>
///// <returns></returns>
//protected string GetCustomPropertyFromCache(string key)
//{
// if (CustomPropertiesCache.ContainsKey(ContextKey))
// {
// StringDictionary plist = CustomPropertiesCache[ContextKey];
// if (plist.ContainsKey(key))
// return plist[key];
// else
// return "";
// }
// else
// {
// return "";
// }
//}
//public override void OnAdded(SPAddFieldOptions op)
//{
// base.OnAdded(op);
// Update();
//}
//public override void Update()
//{
// base.SetCustomProperty(DEFAULT_EMAIL_PROPERTY_NAME, this.GetCustomPropertyFromCache(DEFAULT_EMAIL_PROPERTY_NAME));
// base.Update();
//}
#endregion
}
}
Feture 配置文件,文件名必须以fldtypes开始。
View Code
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">Email</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Email</Field>
<Field Name="TypeShortDescription">Email</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="FieldEditorUserControl"></Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="Filterable">FALSE</Field>
<Field Name="FieldTypeClass">FieldControlsDevelop.EmailField,FieldControlsDevelop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a06fa8821dd50e33</Field>
<PropertySchema>
<Fields>
<Field Name="DefaultEmail" DisplayName="DefaultEmail" MaxLength="300" DisplaySize="30" Type="Text" Hidden="TRUE" >
</Field>
</Fields>
</PropertySchema>
<RenderPattern Name="DisplayPattern">
<HTML><![CDATA[<a href='mailto:"]]></HTML>
<Column/>
<HTML><![CDATA['>]]></HTML>
<Column/>
<HTML><![CDATA[</a>]]></HTML>
</RenderPattern>
</FieldType>
</FieldTypes>
在上面Feature可以看显示逻辑是在<RenderPattern Name="DisplayPattern"></RenderPattern>中定义的,但有些情况下开发人员更喜欢用代码逻辑来处理字段的显示。
这时可以重载Render方法,代码如下:
View Code
protected override void Render(HtmlTextWriter output)
{
if (this.ControlMode == SPControlMode.Display)
{
string email = "" + this.ItemFieldValue;
if (email != "")
{
output.Write("<a href='mailto:");
output.Write(email);
output.Write("'>");
output.Write(email);
output.Write("</a>");
}
}
else
{
base.Render(output);
}
}
布署:
1. 强命名
2. 把Feature文件放到12\TEMPLATE\XML
3. 把DLL文件拖到GAC里.
4. 重启IIS