List and content type in SharePoint

Source: Internet
Author: User

Custom Field Types

================================

In the previous section, you can see an example of using WSS's built-in field type (text, choice, and notes) to create a custom site column definitions. WSS 3.0 also introduces the ability to work at a lower level. You can define your custom M field type. the motivation for doing so is to gain a higher level of control over the underlying initialization, rendering, and Data Validity verification of the column.

For example, you need to create a column for a custom list, which shows a user and has a drop-down list, the data is obtained from a backend database or web service. another scenario is that you need a column, where the value is limited to the validity verification logic that must satisfy domain-specific. This logic is hosted by you.Code(C # Or Visual Basic. Net). In these scenarios, it is reasonable to create a custom field type.

Custom Field Type represents the new data type of a column. Custom field types is attractive to. NET developers because they can be compiledProgramSet and then deploy it to GAC. in addition to the initialization and validity verification of the managed code, the Custom field type is still in one or more ASP. net server-side controls. This gives you the ability to control rendering and allows you to use standard ASP. NET development.

To create a custom field type, you should create a new class library project in Visual Studio. the instance code in this chapter already contains an example project named litwarefieldtypes, which provides all the code required to implement and deploy two M field types. note that the litwarefieldtypes project is configured to compile to its own output with a strong name, so it can be deployed to GAC. deploying to GAC is required to deploy and test custom field type.

The source file contains a file named litwarefieldtypes. CS code file. Two managed classes are used to define the Custom Field Type named companysize. the first class is called companysizefield, which is used to define the Custom Field Type itself. this class inherits from the spfieldtext class, which is defined in the core WSS Assembly Microsoft. sharepoint. in DLL. the second class is companysizefieldcontrol. this class is used to create and initialize an ASP. net dropdownlist control, this control will give us a custom rendering behavior of Custom field type. note that the companysizefieldcontrol class inherits from Microsoft. sharepoint. A class named basefieldcontrol in DLL.

Tip:

For the complete Custom Field Type Base classes (M field type base class), refer to the "Custom Field Type classes" topic in the SDK. All field types are namedSpfieldStart with Boolean, choice, currency, date, text, URL, and multiple column fields.

The following code belongs to the companysizefield class. this class provides two standard constructors used by all custom field types. there is also a public read-only attribute called fieldrenderingcontrol, which is used to create an instance of the companysizefieldcontrol class. finally, there is a method named getvalidatedstring that is rewritten. you can override this method to add your own custom validity verification logic. in our example, we simply verified whether the code is empty. however, you should add additional verification logic to meet your complex business application needs.

The companysizefield class

 Public class  Companysizefield : Spfieldtext { // Each field type requires two standard Constructors  Public Companysizefield (spfieldcollection fields, String Fieldname ): Base (Fields, fieldname ){} Public Companysizefield (spfieldcollection fields, String Typename, String Displayname ): Base (Fields, typename, displayname ){} // Public property used to instantiate control used for rendering  Public override Basefieldcontrol fieldrenderingcontrol { Get {Basefieldcontrol control = New Companysizefieldcontrol (); control. fieldname = This . Internalname; Return Control ;}} // Standard method override used to add validation logic  Public override string Getvalidatedstring ( Object Value ){ If ( This . Required | value. tostring (). Equals ( String . Empty )){ Throw new Spfieldvalidationexception ( "Company size not assigned" );} Return Base . Getvalidatedstring (value );}}

Next, let's take a look at how companysizefieldcontrol is written to facilitate the companysizefieldcontrol. ASP.. Net user control to work collaboratively. specifically, the companysizefieldcontrol class uses a control named renderingtemplate, which is defined in companysizefieldcontrol. in ascx. you should also note that in install. in bat, the companysizefieldcontrol command is also written. the ascx file is copied to the template \ controltemplates directory to be deployed. as you can see, companysizefieldcontrol. the definition of the renderingtemplate control in ascx is not very complex.

 <  SharePoint: renderingtemplate  ID  = "Companysizefieldcontrol " Runat  = " Server " > <  Template  > <  ASP: dropdownlist  ID  = " Companysizeselector " Runat  = " Server " /> </ Template  > </  SharePoint: renderingtemplate  > 

 

Now let's take a look at the following companysizefieldcontrol implementation. this field is called companysizeselector in Aohan. It is based on ASP named dropdownlist. net Control. also, in the createchildcontrols method, the Code also defines the field and the field in companysizefieldcontrol. the dropdownlist instance in ascx is bound. this allows the code in this class to use a series of items to instantiate the dropdownlist control.

 

The companysizefieldcontrol class

 Public class  Companysizefieldcontrol : Basefieldcontrol { Protected Dropdownlist companysizeselector; Protected override string Defaulttemplatename { Get { Return  "Companysizefieldcontrol" ;}} Public override object Value { Get {Ensurechildcontrols (); Return this . Companysizeselector. selectedvalue ;} Set {Ensurechildcontrols (); This . Companysizeselector. selectedvalue = ( String ) This . Itemfieldvalue ;}} Protected override void Createchildcontrols (){ If ( This . Field = Null | This . ControlMode = spcontrolmode. Display) Return ; Base . Createchildcontrols (); This . Companysizeselector = (dropdownlist) templatecontainer. findcontrol ( "Companysizeselector" ); If (This . Companysizeselector = Null ) Throw new Configurationerrorsexception ( "Error: cannot load. ascx file! " ); If (! This . Page. ispostback ){ This . Companysizeselector. Items. addrange ( New Listitem [] { New Listitem ( String . Empty, Null ),New Listitem ( "Mom and Pop Shop (1-20 )" , "1-20" ), New Listitem ( "Small Business (21-100 )" , "21-100" ), New Listitem ( "Medium-sized business (101-1000 )" , 101-1000" ), New Listitem ( "Big Business (1001-20,000 )" ,1001-20000" ), New Listitem ( "Enterprise Business (over 20,000 )" , "20000 +" )});}}}

 

The companysizefieldcontrol class overwrites a read-only attribute named defaulttemplatename, Which is returned in companysizefieldcontrol. the name of the renderingtemplate defined in ascx. code in the base class of the companysizefieldcontrol class uses this string to run from companysizefieldcontrol. ascx loads renderingtemplate.

You should also check the value attribute overwritten in the companysizefieldcontrol class. the internal get method returns the value from the dropdownlist control. the internal set method uses the itemfieldvalue attribute defined in the base class to assign this value to the dropdownlist control.

Now you have seen how two classes and a renderingtemplate in the ascx File work together to provide a Custom Field Type implementation. the last part of this puzzle is to let WSS identify that you have addedField SchemaThe introduction of a new custom field type to the server farm will be copied to a well-known place.

Field types is defined in the name of fldtypes *. in XML files, these files must be deployed in the template \ XML directory. in our example, the field schema named companysize Custom field type is defined in the field schema namedFldtypes_litware.xml. The caml used to define the field schema is listed below. please note that install. bat will install litwarefieldtypes in your compilation project. when the DLL assembly is in GAC, the file is automatically copied to the template \ XML directory. after the project is compiled, you can use the Custom Field Type companysize.

A custom field type definition file

 <?  XML  Version  = " 1.0 "Encoding  = " UTF-8 " ?> <  Fieldtypes  > <  Fieldtype  > <  Field  Name  = " Typename " > Companysize </  Field  > < Field  Name  = " Parenttype " > Text </  Field  > <  Field  Name  = " Typedisplayname " > Company size </  Field  > < Field  Name  = " Typeshortdescription " > Company size </  Field  > <  Field  Name  = " Usercreatable " > True </  Field  > < Field  Name  = " Showinlistcreate " > True </  Field  > <  Field  Name  = " Showinsurveycreate " > True </  Field  > < Field  Name  = " Show.cumentlibrarycreate " > True </  Field  > <  Field  Name  = " Showincolumntemplatecreate " > True </  Field  > < Field  Name  = " Fieldtypeclass " > Litwarefieldtypes. companysizefield, litwarefieldtypes ,... </  Field  > <  Renderpattern  Name  = " Displaypattern " > <  Switch  > < Expr  > <  Column  /> </  Expr  > <  Case  Value  = "" > </  Case  > <  Default  > <  Html  > <! [CDATA [ <Span style = "color: Red"> <B>  ]> </  Html  > <  Column  Subcolumnnumber  = " 0 " Htmlencode  = " True " /> <  Html  > <! [CDATA [  </B> </span> ]> </  Html  > </  Default  > </  Switch  > </  Renderpattern  > </  Fieldtype  > </  Fieldtypes  > 

 

 

After custom field type is deployed in the server farm, you can use it to add a new column to the list, you can also create a new site column on the web browser's WSS interface. presented

Companysize is an example of adding a new column to the list on the standard page provided by WSS.

 

After creating a new custom field type column based on companysize in the list, you can test its appearance and behavior. this example shows how to copy a column based on companysize field type. note: This custom field type is rendered into a dropdownlist control, which displays a series of predefined values. in this example, hard-coded item is used in dropdownlist. You can extend this code to get the item value from other data sources.

 

Translated from:

Inside Microsoft Windows SharePoint Services 3.0 Chapter 6

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.