前段时间,由于刚好项目定制的需要,笔者就开发了几个自定义字段类型。在这抽空做个详细笔记,方便初学者学习
。这方面的资料也很多,如果自身觉得不大明白可以参考下SDK和网上的相关文章。本章的目的主要是给新手起个抛砖引玉的作用。至于字段类型要实现什么功能,还是以自己的实际开发需求来做引导。接下来就介绍该如何实现。。。。
其实实现一个字段类型的开发很简单,无非就是由3部分组成:1继承于SPField的字段类,2继承于BaseRenderControl的字段呈现空间类,3编写一个配置文件,配置文件必须以fldtypes_开头。(如果是采用模版,那就再编写一个.ascx模版页)。
1.BaseRenderControl的常用属性和方法有【
Value:此属性自动被系统调用,可以利用此属性跟字段进行交互。
Field:利用此属性获取跟控件相关联的字段类。
FieldName:获取或设置字段名。ControlMode:获取空间当前所出的模式。
List:获取关联的列表。ItemFieldValue:获取对应列表项的值】。
2.SPField的常用属性和方法有【
ShowInDisplayForm:此属性控制字段是否显示在呈现页面
ShowInEditForm:此属性控制字段是否显示在编辑页面
ShowInNewForm:此属性控制字段是否显示在新建页面
FieldValueType:字段类型值
FieldRenderingControl:返回字段呈现空间实例
GetValidatedString:此方法返回通过验证的值,若验证失败则抛出SPFieldValidationException
GetFieldValue:获得字段的值
GetFieldValueForEdit:用于显示在编辑的页面上
OnAdded:字段被添加之后调用
OnUpdated:字段被更新之后调用
OnDeleting:字段被删除之后调用】
3.至于配置文件:大家可以直接参考SDK相关资料。。。。。。。。这里就不在详细简述了。
下面直接就把相关的代码文件附上:(该字段类型实现获取当前网站的所有调查列表的名称。至于中间用到的一些方法,大伙如果不明白,可以直接看代码中的注释。新建一个类库项目,两个类文件分别命GetallSurveyListTitleFielControl,GetallSurveyListTitleField,然后引用Microsoft.Sharepoint.dll,项目记得设置强名称。。。。。。。。。。。。。)
GetallSurveyListTitleFielControl.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using Microsoft.SharePoint;
5
using Microsoft.SharePoint.WebControls;
6
using System.Web;
7
using System.Web.UI.WebControls;
8
using System.IO;
9
namespace GetallSurveyListTitle
10
GetallSurveyListTitleField.cs
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using Microsoft.SharePoint;
5
using Microsoft.SharePoint.WebControls;
6
namespace GetallSurveyListTitle
7
fldtypes_GetallSurveyListTitle.xml
1
<?xml version="1.0" encoding="utf-8"?>
2
<FieldTypes>
3
<FieldType>
4
<Field Name="TypeName">GetallSurveyListTitle</Field>
5
<Field Name="ParentType">Choice</Field>
6
<Field Name="TypeDisplayName">获取调查列表标题</Field>
7
<Field Name="TypeShortDescription">获取调查列表标题</Field>
8
<Field Name="UserCreatable">TRUE</Field>
9
<Field Name="ShowInListCreate">TRUE</Field>
10
<Field Name="ShowInSurveyCreate">TRUE</Field>
11
<Field Name="ShowInDocumentLibraryCreate">TRUE</Field>
12
<Field Name="ShowInColumnTemplateCreate">TRUE</Field>
13
<Field Name="FieldTypeClass">GetallSurveyListTitle.GetallSurveyListTitleField,GetallSurveyListTitle,Version=1.0.0.0, Culture=neutral, PublicKeyToken=d983c60297f46343</Field>
14
</FieldType>
15
</FieldTypes>
16
17
GetallSurveyListTitleFielControl.ascx
1
<%@ Control Language="C#" AutoEventWireup="false" %>
2
<%@Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
3
<%@Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" namespace="Microsoft.SharePoint.WebControls"%>
4
<SharePoint:RenderingTemplate ID="SurveyListTitleFieldControl" runat="server">
5
<Template>
6
<asp:DropDownList runat="server" ID="Drp_SurveyListTitle" />
7
</Template>
8
</SharePoint:RenderingTemplate>
9
<SharePoint:RenderingTemplate ID="SurveyListTitleDisplay" runat="server">
10
<Template>
11
<asp:Label runat="server" ID="lb_SurveyListTitle" />
12
</Template>
13
</SharePoint:RenderingTemplate>
到此一个字段类型开发完毕。
部署:1.把项目生成的.dll文件放入GAC。
2.把配置文件放入12\Template\XML
3.把字段模版文件放入12\Template\ControlTemplate
4.IISRESET
相关文章: