1. 程式人生 > >sharepoint 2010 自定義欄位開發(1) 建立一個簡單的列表自定義欄位

sharepoint 2010 自定義欄位開發(1) 建立一個簡單的列表自定義欄位

在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的欄位。

廣州京微資訊科技有限公司,微軟sharepoint解決方案提供商。