[转]SharePoint 2007 如何创建自定义字段类型
2010-05-13 15:08
591 查看
引自:http://vspug.com/nicksevens/2007/08/31/create-custom-field-types-for-sharepoint/
Create a new usercontrol file (customfieldcontrol.ascx). In this usercontrol, add a SharePoint:RenderingTemplate control, and insert whatever you want for your control template.
<%@ Control Language="C#" %>
<%@ Assembly Name="CustomControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:RenderingTemplate Id="CustomFieldRendering" runat="server">
<Template>
<asp:TextBox runat="server" ID="txtFirstName" /> <asp:TextBox runat="server" ID="txtLastName" />
</Template>
</SharePoint:RenderingTemplate>
Secondly, create a new class file (customfieldcontrol.cs), which will take the role of codebehind file of the usercontrol. In this class, which will inherit from Microsoft.SharePoint.WebControls.BaseFieldControl, we will override some of the properties and methods to implement our own logic.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomControl
{
public class customfieldcontrol : BaseFieldControl
{
protected TextBox txtFirstName;
protected TextBox txtLastName;
protected override string DefaultTemplateName
{
get { return "CustomFieldRendering"; }
}
public override object Value
{
get
{
EnsureChildControls();
return txtFirstName.Text + "%" + txtLastName.Text;
}
set
{
try
{
EnsureChildControls();
txtFirstName.Text = value.ToString().Split('%')[0];
txtLastName.Text = value.ToString().Split('%')[1];
}
catch { }
}
}
public override void Focus()
{
EnsureChildControls();
txtFirstName.Focus();
}
protected override void CreateChildControls()
{
if (Field == null) return;
base.CreateChildControls();
//Don't render the textbox if we are just displaying the field
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display) return;
txtFirstName = (TextBox)TemplateContainer.FindControl("txtFirstName");
txtLastName = (TextBox)TemplateContainer.FindControl("txtLastName");
if (txtFirstName == null) throw new NullReferenceException("txtFirstName is null");
if (txtLastName == null) throw new NullReferenceException("txtLastName is null");
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.New)
{
txtFirstName.Text = "";
txtLastName.Text = "";
}
}
}
}
Actually, what this code does is:
Define the ID of the renderingtemplate control in our usercontrol.
Set the return value to the value we want it to return
, in this case this will be something like Firstname%Lastname.
Make sure that when you edit an item, the proper values are filled in into the textboxes again.
Next thing to do is to create the Field type class (CustomField.cs) itself. In this class, which derives of one of the base control types from SharePoint (SPFieldText, SPFieldChoice, …), we will define which control has to be used as template, and which value has to be returned when displaying a list item.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomControl
{
public class CustomField : SPFieldText
{
public CustomField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{ }
public CustomField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{ }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new customfieldcontrol();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
public override string GetValidatedString(object value)
{
return value.ToString().Split('%')[1].ToUpper() + " " + value.ToString().Split('%')[0];
}
}
}
Main thing of this code is the GetValidatedString() function. This function defines what is to be displayed when you display an item (in a list view for example).
In our case, which in our case will be something like LASTNAME Firstname.
Last file to create is the XML File (fldtypes_custom.xml), which will add the custom field type to SharePoint.
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">CustomField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Custom Name Field</Field>
<Field Name="TypeShortDescription">Custom Name Text Field</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListCreate">TRUE</Field>
<Field Name="ShowOnSurveyCreate">TRUE</Field>
<Field Name="ShowOnDocumentLibrary">TRUE</Field>
<Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="Filterable">TRUE</Field>
<Field Name="FieldTypeClass">CustomControl.CustomField, CustomControl, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx</Field>
<Field Name="SQLType">nvarchar</Field>
</FieldType>
</FieldTypes>
So far so good with creating the files
Now, all we have to do is put the right files in the right places …
Compile your project (or at least both .cs classes) and make sure they are strong named.
Then, install them in the GAC (%windir%assembly)
Next, copy the .ascx file to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES
And the last file to copy: copy the .xml file to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML
That's it. Maybe do an IISRESET, and you can use your newly created Custom Field Type!
Create custom field types for SharePoint 2007
In this example we are creating a custom field type which concatenates two text fields (first and last name).Create a new usercontrol file (customfieldcontrol.ascx). In this usercontrol, add a SharePoint:RenderingTemplate control, and insert whatever you want for your control template.
<%@ Control Language="C#" %>
<%@ Assembly Name="CustomControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" %>
<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:RenderingTemplate Id="CustomFieldRendering" runat="server">
<Template>
<asp:TextBox runat="server" ID="txtFirstName" /> <asp:TextBox runat="server" ID="txtLastName" />
</Template>
</SharePoint:RenderingTemplate>
Secondly, create a new class file (customfieldcontrol.cs), which will take the role of codebehind file of the usercontrol. In this class, which will inherit from Microsoft.SharePoint.WebControls.BaseFieldControl, we will override some of the properties and methods to implement our own logic.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomControl
{
public class customfieldcontrol : BaseFieldControl
{
protected TextBox txtFirstName;
protected TextBox txtLastName;
protected override string DefaultTemplateName
{
get { return "CustomFieldRendering"; }
}
public override object Value
{
get
{
EnsureChildControls();
return txtFirstName.Text + "%" + txtLastName.Text;
}
set
{
try
{
EnsureChildControls();
txtFirstName.Text = value.ToString().Split('%')[0];
txtLastName.Text = value.ToString().Split('%')[1];
}
catch { }
}
}
public override void Focus()
{
EnsureChildControls();
txtFirstName.Focus();
}
protected override void CreateChildControls()
{
if (Field == null) return;
base.CreateChildControls();
//Don't render the textbox if we are just displaying the field
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.Display) return;
txtFirstName = (TextBox)TemplateContainer.FindControl("txtFirstName");
txtLastName = (TextBox)TemplateContainer.FindControl("txtLastName");
if (txtFirstName == null) throw new NullReferenceException("txtFirstName is null");
if (txtLastName == null) throw new NullReferenceException("txtLastName is null");
if (ControlMode == Microsoft.SharePoint.WebControls.SPControlMode.New)
{
txtFirstName.Text = "";
txtLastName.Text = "";
}
}
}
}
Actually, what this code does is:
Define the ID of the renderingtemplate control in our usercontrol.
Set the return value to the value we want it to return
, in this case this will be something like Firstname%Lastname.
Make sure that when you edit an item, the proper values are filled in into the textboxes again.
Next thing to do is to create the Field type class (CustomField.cs) itself. In this class, which derives of one of the base control types from SharePoint (SPFieldText, SPFieldChoice, …), we will define which control has to be used as template, and which value has to be returned when displaying a list item.
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace CustomControl
{
public class CustomField : SPFieldText
{
public CustomField(SPFieldCollection fields, string fieldName)
: base(fields, fieldName)
{ }
public CustomField(SPFieldCollection fields, string typeName, string displayName)
: base(fields, typeName, displayName)
{ }
public override BaseFieldControl FieldRenderingControl
{
get
{
BaseFieldControl fieldControl = new customfieldcontrol();
fieldControl.FieldName = this.InternalName;
return fieldControl;
}
}
public override string GetValidatedString(object value)
{
return value.ToString().Split('%')[1].ToUpper() + " " + value.ToString().Split('%')[0];
}
}
}
Main thing of this code is the GetValidatedString() function. This function defines what is to be displayed when you display an item (in a list view for example).
In our case, which in our case will be something like LASTNAME Firstname.
Last file to create is the XML File (fldtypes_custom.xml), which will add the custom field type to SharePoint.
<?xml version="1.0" encoding="utf-8"?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">CustomField</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Custom Name Field</Field>
<Field Name="TypeShortDescription">Custom Name Text Field</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="ShowOnListCreate">TRUE</Field>
<Field Name="ShowOnSurveyCreate">TRUE</Field>
<Field Name="ShowOnDocumentLibrary">TRUE</Field>
<Field Name="ShowOnColumnTemplateCreate">TRUE</Field>
<Field Name="Sortable">TRUE</Field>
<Field Name="Filterable">TRUE</Field>
<Field Name="FieldTypeClass">CustomControl.CustomField, CustomControl, Version=1.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx</Field>
<Field Name="SQLType">nvarchar</Field>
</FieldType>
</FieldTypes>
So far so good with creating the files
Now, all we have to do is put the right files in the right places …
Compile your project (or at least both .cs classes) and make sure they are strong named.
Then, install them in the GAC (%windir%assembly)
Next, copy the .ascx file to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\CONTROLTEMPLATES
And the last file to copy: copy the .xml file to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML
That's it. Maybe do an IISRESET, and you can use your newly created Custom Field Type!
相关文章推荐
- 如何在SharePoint 2010中创建自定义字段类型
- 如何用VS2010在SharePoint中创建自定义字段类型(以eWebEditor为例)
- 如何用VS2010在SharePoint中创建自定义字段类型(以eWebEditor为例)
- 在SharePoint 2010中创建自定义字段类型
- msdn创建自定义 SharePoint 2010 字段类型
- 一步一步SharePoint 2007之四十二:实现自定义Workflow(1)——在VS中创建一个Workflow
- sharepoint 2010 如何扩展webpart自定义属性边栏字段 custom webpart properties
- SharePoint 2010 自定义 字段 类型--------省市区联动
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- 【VSTO】创建 Excel 2007 AddIn (1. CommandBar 以及如何自定义Icon)
- 通过自定义字段类型实现SharePoint列表的父子关系
- sharepoint 创建自定义字段
- sharepoint 2010 如何扩展webpart自定义属性边栏字段 custom webpart properties
- 在Access中创建表及如何指定字段类型
- 关于学习MSDN中"创建自定义字段类型和字段控件"的一些心得跟问题。
- Sharepoint SP1下创建自定义字段应注意的问题
- 创建自定义字段类型
- MySQL中日期类型数据的使用:如何让字段保存记录的创建时间和最后修改时间
- SharePoint 2010 自定义 字段 类型--------三级互联(省县市)
- 如何在sharepoint 2013中创建自定义webpart