開發自定義字段類型 sharepoint

網上有好多類似的文章,但是他們寫的註釋太少了,我研究了一個星期才弄好。

我也寫寫吧,希望對新接觸的朋友有幫助。

首先:寫好一個xml文件

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
  <FieldType>
    <Field Name="TypeName">PeopleSelector</Field>   --類名
    <Field Name="ParentType">Text</Field>   --字段類型
    <Field Name="TypeDisplayName">People Selector</Field> 
    <Field Name="TypeShortDescription">PictureTag</Field>   --顯示名稱
    <Field Name="UserCreatable">TRUE</Field>    
    <Field Name="FieldTypeClass">
      RownSearch(命名空間).PictureSelector(要調用的類名), RownSearch(命名空間), Version=1.0.0.0, Culture=neutral, PublicKeyToken=1a47724c4e1447d0
    </Field>
  </FieldType>
</FieldTypes>

namespace RownSearch
{
    //繼承
    class PeopleSelector : SPFieldText(必須繼承)
    {
        public PeopleSelector(
            SPFieldCollection fields, string fieldName) :
            base(fields, fieldName) { }

        public PeopleSelector(
            SPFieldCollection fields,
        string typeName, string displayName) :
            base(fields, typeName, displayName) { }

         //上面是必寫的
        //兩種方式一種自定義用戶控件
        public override BaseFieldControl FieldRenderingControl
        {
            get
            {
                //轉到定義頁
                BaseFieldControl fieldControl = new PictureSelector_Control();
                fieldControl.FieldName = InternalName;
                return fieldControl;
            }
        }
    }
}

namespace RownSearch
{
    //必須繼承BaseFieldControl
    class PictureSelector_Control : BaseFieldControl
    {
        //自定義控件
        protected TextBox Picture_TextBoxField;
        protected HiddenField Picture_TextBoxFields;
        protected Button Picture_Btn;

        //創建控件
        protected override void CreateChildControls()
        {
            //在編輯和新增下顯示
            if (this.Field == null || this.ControlMode == SPControlMode.Display)
            {
                return;
            }
            base.CreateChildControls();
            //從模版中獲取控件

            Picture_TextBoxField = TemplateContainer.FindControl("Picture_TextBox") as TextBox;
            //獲取button
            Picture_Btn = TemplateContainer.FindControl("btn_Picture") as Button;
        }
       
        //在顯示模式下顯示模版
        //public override string DisplayTemplateName {
        //    get {
        //        return "";
        //    }
        //}
        //在默認模式下顯示模版 //模版SharePoint:RenderingTemplate的id
        protected override string DefaultTemplateName
        {
            get
            {
                return "Picture_FieldTemplate";//用戶控件裏SharePoint:RenderingTemplate的ID
            }
        }
        //重寫value
        public override object Value
        {
            get
            {
                this.EnsureChildControls();
                if (Picture_TextBoxField != null)
                {
                    return Picture_TextBoxField.Text.ToString(); ;
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.EnsureChildControls();
                if (Picture_TextBoxField != null)
                {
                    Picture_TextBoxField.Text = (string)this.ItemFieldValue;
                }
            }
        }
    }

用戶控件

<SharePoint:RenderingTemplate Id="TagTree_FieldTemplate" runat="server" >   
    <Template>
       <script type="text/javascript">
           function OpenToSearchPeople() {
               var options = {
                   url: "/_layouts/showTagTree.aspx",
                   width: 600,
                   height: 480,
                   title: "標籤",
                   dialogReturnValueCallback: TagTreeCloseCallback
               };
               SP.UI.ModalDialog.showModalDialog(options);
           }
           function TagTreeCloseCallback(result, value) {
               debugger;
               if (result == SP.UI.DialogResult.OK) {
                   var txtID = "ctl00_m_g_5a16be22_8c73_4a3f_a3ee_03fe2a571152_ff121_ctl00_ctl00_People_TextBox";
                   document.getElementById(txtID).value += value;
               }
           }
       </script>
       <asp:TextBox  ID="People_TextBox" runat="server"/>

       <asp:Button ID="Button1" runat="server" OnClientClick="javascript:OpenToSearchPeople();return false;" Text="標籤" />
    </Template>
</SharePoint:RenderingTemplate>

用戶控件調一個模式化窗體,這個不是所有人都用的到的

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/javascript">

    function btonTree_ClientClick() {
        var treeTagBack = document.getElementById("<% =this.TreeTagHidden.ClientID %>").value;

        SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, treeTagBack);
    }
</script>
<asp:Button ID="BackTreeTag" runat="server" Text="確定" OnClientClick="javascript:btonTree_ClientClick();" />
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Arrows" OnTreeNodeCheckChanged="TreeView1_TreeNodeCheckChanged" ShowCheckBoxes="All">
            <ParentNodeStyle Font-Bold="False" />
            <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
            <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD" HorizontalPadding="0px" VerticalPadding="0px" />
            <NodeStyle Font-Names="Tahoma" Font-Size="10pt" ForeColor="Black" HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>

 

<asp:HiddenField ID="TreeTagHidden" runat="server"/>
</asp:Content>

裏面有不懂得請給我留言

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章