现在的位置: 首页 > 综合 > 正文

Sharepoint自定义field type

2013年11月19日 ⁄ 综合 ⁄ 共 4902字 ⁄ 字号 评论关闭

 

Sharepoint开发中很多时候都需要你自定义一个field type,只有这样才能满足我们的自定义的需求,并且实现我们对UI的定制,达到我们对其扩展的目的

1.      编写一个xml文件用来描述这个自定义的field type的属性(文件名字需要以fldtypes开头,例如:fldtypes_testfieldtype.xml

2.      编写一个自定义控件(UserControl,例如:TestFieldEditor.ascx),用来在使用这个field type创建column的时候添加一些这种field type所需要的配置数据,例如:一个lookup field需要添加一个List和一个这个List下的column作为这个lookup的数据源。

3.      编写一个sharepoint template,作为这个field type的控件的template,实现定制UI

4.      重写一个sharepoint field type作为这个自定义的field type的基类

5.      重写sharepoint basefieldcontrol作为这个自定义field typecontrol

Fldtypes_testfieldtype.xml如下:

<?xml version="1.0" encoding="utf-8" ?>

<FieldTypes>

       <FieldType>

              <Field Name="TypeName">ExLookField</Field>

              <Field Name="TypeDisplayName">ExLookField </Field>

              <Field Name="TypeShortDescription">Extesion of lookup field </Field>

    <Field Name="TypeDescription">This column allows for…..</Field>

              <Field Name="ParentType">Lookup</Field>

    <Field Name="FieldEditorUserControl">/_controltemplates/TestFieldEditor.ascx</Field>

    <Field Name="FieldTypeClass">

                     TestField,

                     TestField,

                     Version=1.0.0.0,

                     Culture=neutral,

                     PublicKeyToken=8ce44e7ccb44bd10</Field>

    <RenderPattern Name="DisplayPattern">

      <LookupColumn HTMLEncode="TRUE" AutoHyperLink="FALSE" />

    </RenderPattern>

  </FieldType>

</FieldTypes>

TestFieldEditor.ascx代码如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="TestFieldEditor.ascx.cs" Inherits="TestFieldEditor, TestFields, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8ce44e7ccb44bd10" %>

<%@ Register TagPrefix="wssuc" TagName="InputFormControl" src="~/_controltemplates/InputFormControl.ascx" %>

<%@ Register TagPrefix="wssuc" TagName="InputFormSection" src="~/_controltemplates/InputFormSection.ascx" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<wssuc:InputFormSection runat="server" id="TestSections" Title="Special Configuration Section">

       <Template_InputFormControls>

             <wssuc:InputFormControl runat="server"

                    LabelText="Test。。。。">

                    <Template_Control>

                        <sharepoint:PeopleEditor ID="AllowedPrincipalsPeoplePicker" runat="server" AutoPostBack="false" PlaceButtonsUnderEntityEditor="true" SelectionSet="SPGroup" MultiSelect="true" />

                         <asp:Button runat="server" Text="TestButton"/>

                    </Template_Control>

             </wssuc:InputFormControl>

       </Template_InputFormControls>

</wssuc:InputFormSection>

自定义sharepoint template 如下:

<%@ Control Language="C#" %>

<%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register TagPrefix="SharePoint" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"

             Namespace="Microsoft.SharePoint.WebControls" %>

 

<SharePoint:RenderingTemplate ID="TestLookupFieldControl" runat="server">

  <Template>

        <asp:TextBox ID="TbTest" runat="server"></asp:TextBox>

        <asp:ImageButton ID="IBTest" runat="server" />

         <asp:Button ID="BtnTest" runat="server" />

  </Template>

</SharePoint:RenderingTemplate>

自定义的field type(继承SPFieldLookup,代码如下:

public class ExLookupField : SPFieldLookup

    {

        publicExLookupField(SPFieldCollection fields, string fieldName)

            : base(fields, fieldName) { }

 

        public ExLookupField(SPFieldCollection fields, string typeName, string displayName)

            : base(fields, typeName, displayName) { }

 

        public override Microsoft.SharePoint.WebControls.BaseFieldControl FieldRenderingControl

        {

            get

            {

                BaseFieldControl control = new TestLookupControl();

                control.FieldName = this.InternalName;

                return control;

            }

        }

 

        public override string GetValidatedString(object value)

        {

            if (value == null)

            {

                throw new SPFieldValidationException("value is null.");

            }

            return string.Empty;

        }

}

编写一个自定义的control(继承BaseFieldControl)其实现如下:

public class LookupControl : BaseFieldControl

    {

        protected TextBox mCompanySizeSelector;

        protected ImageButton mImage;

        protected Button mButton;

 

        protected override string DefaultTemplateName

        {

            get

            {

                return @"TestLookupFieldControl";

            }

        }

 

 

        public override object Value

        {

            get

            {

               Return ;

            }

            set

            {

             

            }

      }

protected override void CreateChildControls()

{

   base.CreateChildControls();

}

以上就是对自定义field type的开发的整个流程的介绍,并且对需要的文件做详细的介绍,可以在相应的文件中添加自己的code实现自定义的逻辑,

    完成所需要的功能。

 

抱歉!评论已关闭.