Sharepoint Custom Field Type

Source: Internet
Author: User

Moss2007 has many common types by default, such as text, date, number, and option... , Sometimes we need to customize a type as needed! In WSS3.0, you can use C # Or VB to write extended fields. All fields automatically need to inherit the built-in Sharepoint types (such as SPFieldText and SPFieldNumber)

Field Type Structure

    • Field Type
    • Field Value class (not required)
    • Field display (user control or direct output)
    • Field description file (the file must start with fldtypes)

Demo:

Open VS2008, create a Sharepoint-type "Empty" project "CustomFieldsDemo", add --> new item, select the Field Control type, and name it "ddlTree ", the XML file for field description is also automatically generated!

Open ddlTree. Field. cs and change the inheritance to "SPFieldChoice ";

Open ddlTree. FieldControl. cs and change inheritance to "BaseFieldControl". Write the logic code according to your field requirements!

DdlTree. fieldControl. csusing System; using System. data; using System. runtime. interopServices; using System. web. UI. webControls; using Microsoft. sharePoint; using Microsoft. sharePoint. webControls; namespace CustomFieldsDemo {[CLSCompliant (false)] [Guid ("Courier")] public class ddlTreeFieldControl: BaseFieldControl // TextField {protected DropDownList ddlTree; protected overrid E string DefaultTemplateName {get {// user control name, <SharePoint: RenderingTemplate> the Control ID must be equal to this value return "ddlTreeFieldRendering ";}} public override object Value {get {EnsureChildControls (); return ddlTree. selectedValue;} set {EnsureChildControls (); ddlTree. selectedValue = this. itemFieldValue + "" ;}} public override void Focus () {EnsureChildControls (); ddlTree. focus ();} protected override void CreateChildControls () {if (Field = null) return; base. createChildControls (); if (ControlMode = SPControlMode. display) return; // Control ID: ddlTree = (DropDownList) TemplateContainer. findControl ("ddlTree"); if (ddlTree = null) throw new ArgumentException ("ddlTree control not found"); ddlTree. toolTip = Field. title + ""; ddlTree. tabIndex = TabIndex; ddlTree. cssClass = CssClass; if (! Page. isPostBack) BindData ();} // List Name: Tree private string sourceTreeList = "Tree"; private void BindData () {SPWeb web = SPControl. getContextWeb (Context); SPList treeList = web. lists [sourceTreeList]; SPListItemCollection items = treeList. items; ddlTree. items. add (new ListItem ("set as Level 1 Classification", "0"); DataTable dt = items. getDataTable (); if (dt. rows. count> 0) {foreach (DataRow row in dt. rows) {if (row ["ParentID"]. toString (). trim () = "0") {ddlTree. items. add (new ListItem (row ["ClassName"] + "", row ["ClassID"] + ""); BindChildData (dt, row ["ClassID"] + "", 1) ;}}} private void BindChildData (DataTable dt, string classID, int spaceLength) {DataRow [] rows = dt. select ("ParentID =" + classID, "ClassID"); foreach (DataRow row in rows) {ddlTree. items. add (new ListItem (SpaceLength (spaceLength) + row ["ClassName"] + "", row ["ClassID"] + ""));}} private string SpaceLength (int spaceLength) {string space = ""; for (int I = 0; I <spaceLength; I ++) {space + = "";} return space + "empty ";}}}

 

 

Create a display control for the field type (here, create a user control)

<%@ Control Language="C#" Debug="true" %><%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <SharePoint:RenderingTemplate ID="ddlTreeFieldRendering" runat="server">    <Template>        <asp:DropDownList ID="ddlTree" runat="server"></asp:DropDownList>    </Template></SharePoint:RenderingTemplate>
 
The file is saved as: ddlTreeFieldRendering. ascx. The file name is consistent with the ID name of the RenderingTemplate control and the DefaultTemplateName attribute value!

 

Content in fldtypes_ddlTree.xml

Fldtypes_ddlTree.xml <? Xml version = "1.0" encoding = "UTF-8"?> <FieldTypes> <FieldType> <Field Name = "TypeName"> drop-down list </Field> <Field Name = "ParentType"> Choice </Field> <Field Name = "TypeDisplayName"> This is a drop-down list </Field> <Field Name = "TypeShortDescription"> multi-level drop-down list </Field> <Field Name = "UserCreatable"> TRUE </Field> <field Name = "UserCreatable"> TRUE </Field> <Field Name = "ShowOnListAuthoringPages"> TRUE </Field> <Field Name = "ShowOnSurveyAuthoringPages"> TRUE </Field> <Field name = "ShowOnDocumentLibraryAuthoringPages"> TRUE </Field> <Field Name = "ShowOnColumnTemplateAuthoringPages"> TRUE </Field> <Field Name = "FieldTypeClass"> CustomFieldsDemo. ddlTreeField, CustomFieldsDemo, Version = 1.0.0.0, Culture = neutral, publicKeyToken = 9f4da00rjc38ec5 </Field> <RenderPattern Name = "DisplayPattern"> <Switch> <Expr> <Column/> </Expr> <Case Value = ""> </Case> <Default> <Column SobColumnNumber = "0" HTMLEn Code = "TRUE"/> <HTML> <! [CDATA [<BR>]> </HTML> <Column SobColumnNumber = "1" HTMLEncode = "TRUE"/> <HTML> <! [CDATA [& NBSP;-& NBSP;]> </HTML> <Column SobColumnNumber = "2" HTMLEncode = "TRUE"/> <HTML> <! [CDATA [<BR>]> </HTML> </Default> </Switch> </RenderPattern> </FieldType> </FieldTypes>

 

Open your site and create a new List named "Tree". The effect is as follows, because the above code needs to obtain the data in the Tree, and the fields must be the same.

 

Deployment, use:

  1. Copy the "ddlTreeFieldRendering. ascx" file to "C: \ Program Files \ Common Files \ microsoft shared \ Web Server Extensions \ 12 \ TEMPLATE \ CONTROLTEMPLATES ";
  2. Copy the fldtypes_ddlTree.xml file to C: \ Program Files \ Common Files \ microsoft shared \ Web Server Extensions \ 12 \ TEMPLATE \ XML
  3. Register the assembly of the project to GAC and restart IIS! You can use gacutil-if d: dll. dll in vs Tools to register with GAC and iisreset to restart IIS.

Finally:

 

Reference books: SharePoint Server 2007 case studies by Yang Yonggang

 

Technorati Tag: sharepoint, Custom Field Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.