在sharepoint 2010 中,最常用的就是对自定义列表或者文档库的使用,创建一个自定义列表或者文档库,添加一些需要的字段,sharepoint 2010 自带了很多不同类型的控件供字段使用,如下图
很多特殊情况下,这些类型控件,不一定能满足我们的需求,所以我们需要扩展更多的控件类型。
1。创建一个framework 3.5的sharepoint project,名称为 CustomFieldTest
添加一个sharepoint 映射文件夹,展开template,添加controltemplates和xml文件夹
选择controltemplates文件夹 |
选择xml文件夹 |
添加完成后,如下图
2。在controltemplates文件夹下,添加一个新建项
选择用户控件,名称为CustomFieldTest.TextBoxControlTemplate.ascx,点击添加
3。在该文件中,添加一个renderingtemplate,Template中添加一个TextBox,在代码如下:
<sharepoint:renderingtemplate id="txtControlRenderingTemplate" runat="server">
<Template>
<asp:TextBox ID="txtControl" TextMode="SingleLine" runat="server"></asp:TextBox>
</Template>
</sharepoint:renderingtemplate>
4。在xml文件夹中,添加一个xml文件,名称为 fldtypes_CustomFieldTest.xml,这里的名称有一定的规则,就是必须以fldtypes_开头,后面一般是用应用程序名称命名。
代码如下:
<?xmlversion="1.0"encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<FieldName="TypeName">TxtControl</Field>
<FieldName="ParentType">Text</Field>
<FieldName="TypeDisplayName">TxtControl</Field>
<FieldName="TypeShortDescription">TxtControl</Field>
<FieldName="UserCreatable">TRUE</Field>
<FieldName="ShowInColumnTemplateCreate">TRUE</Field>
<FieldName="FieldTypeClass">CustomFieldTest.TextBoxControl,CustomFieldTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=3e32063bdc8e93a7</Field>
</FieldType>
</FieldTypes>
5。创建一个class文件,名称为TextBoxControl.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
namespace CustomFieldTest
{
public class TextBoxControl : SPFieldText
{
public TextBoxControl(SPFieldCollection fields, string fieldName)
: base(fields, fieldName) { }
public TextBoxControl(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName) { }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl ctr = new TextBoxControlFieldControl();
ctr.FieldName = this.InternalName;
return ctr;
}
}
}
}
public class TextBoxControlFieldControl : BaseFieldControl
{
protected override string DefaultTemplateName
{
get
{
return "txtControlRenderingTemplate";
}
}
protected TextBox txtControl;
protected override void CreateChildControls()
{
base.CreateChildControls();
txtControl = (TextBox)TemplateContainer.FindControl("txtControl");
}
public override object Value
{
get
{
this.EnsureChildControls();
return txtControl.Text;
}
set
{
this.EnsureChildControls();
txtControl.Text = ItemFieldValue.ToString();
}
}
}
}
6。部署该程序,我们打开一个自定义列表或者文档库,在创建栏那里,我们就能看到多了一个TextControl控件类型,填写一个test栏位
点击保存后,就在列表中多了一个叫txtControl的字段。
附件在文档里面/CustomFieldTest.zip