网站栏已经为一般用户和开发者提供了非常好的可重用的能力。更进一步,你可以定义一个可重用的栏定义,这将为我们带来更大的灵活性。配合Microsoft SharePoint Foundation 2010,你可以将创建自定义字段类型的门槛降得很低。
下面是创建一个自定义字段类型的总体步骤。你也可以参考这个msdn How to Video 视频。
创建一个自定义字段控件的过程为:
1. 创建一个public 的自定义字段类型的类,并继承自某个内置的字段类型类,比如SPFieldBoolen,SPFieldChoice,或 SPFieldText。
2. 创建两个public的构造器,接收特定的参数,并传递给相同参数的父类的构造器。
3. 创建一个XML文件,作为该字段类型的部署文件。你必须将其部署到一个指定的目录中,并且需要在场级别激活该自定义字段类型。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace WingtipCustomFields {
public class ProductCode : SPFieldText {
public ProductCode(SPFieldCollection fields, string fName) : base(fields, fName) { }
public ProductCode(SPFieldCollection fields, string tName, string dName) : base(fields, tName, dName) { }
public override string DefaultValue {
get { return “P001″; }
}
public override string GetValidatedString(object value) {
if (!value.ToString().StartsWith(“P”))
{ throw new SPFieldValidationException(“Product code must start with ‘P’”); }
if (value.ToString().Length != 4)
{ throw new SPFieldValidationException(“Product code must be 4 chars”); }
// 在写入内容数据库前总是转换成大写的
return value.ToString().ToUpper();
}
}}