I found a lot of information on the Internet, but I didn't have a custom field type for Treeview. So I wrote one myself, and the effect was good. Share ^_^
1. Create an empty SharePoint project in vs2005 and set the project name.
2. Add a new item to the project, select "field control", fill in the project name (for example, myfield), and click OK.
3. At this time, there will be several more folders and class files in the project. There is a myfield folder (Note: This name matches the name in step 1). There are two Cs files in this folder: myfield. Field. CS, myfield. fieldcontrol. CS
These two files have their own functions.
1. myfield. Field. CS is the main class of this custom field type and must inherit from spfield or its subclass.
Because my Treeview requires multiple selections, It inherits the spfieldmultichoice
Some methods are provided by default. The two constructors do not need to be changed. What we need to do is override the getfieldvalue method and the getvalidatedstring method. The first is that the user returns the value of the Custom field type, and the second is to verify that the input is correct.
Public override object getfieldvalue (string value)
{
Return new spfieldmultichoicevalue (value );
}
Public override string getvalidatedstring (object value)
{
If (base. Required & value. tostring (). Equals (string. Empty ))
{
Throw new spfieldvalidationexception ("not allowed! ");
}
Return Value. tostring ();
}
2. myfield. fieldcontrol. CS is a class that controls display of Custom field types. A control template must be loaded. This template can be selected by default or customized by yourself. Because you need to write a Treeview, you can customize one.
First, the class should inherit the basefieldcontrol class, And the defaulttemplatename attribute, value attribute, and createchildcontrols method must be rewritten. The first attribute is used to find the template of the control, that is, an ascx control, which is stored in the 12 \ template \ controltemplates directory. The second attribute is used to read and write the value of the Custom field type. The last method is necessary to create the control.
Protected override string defaulttemplatename
{
Get
{
Return @ "mytreeview ";
}
}
Public override object Value
{
Get
{
Ensurechildcontrols ();
Spfieldmultichoicevalue fieldmultichoicevalue = new spfieldmultichoicevalue ();
Gettreeviewcheckedvalue (Treeview. nodes [0], ref fieldmultichoicevalue );
Return fieldmultichoicevalue;
}
Set
{
This. ensurechildcontrols ();
This. setfieldcontrolvalue (value );
}
}
Protected override void createchildcontrols ()
{
If (this. Field = NULL | this. ControlMode = spcontrolmode. Display)
Return;
Base. createchildcontrols ();
Treeview TV = (Treeview) templatecontainer. findcontrol ("Treeview ");
Buildtree (TV, getdata ());
}
4. Create a custom control for display (ascx)
<% @ Control Language = "C #" DEBUG = "true" %>
<% @ 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 = "mytreeview" runat = "server">
<Template>
<Asp: Treeview id = "Treeview" runat = "server" font-size = "10pt">
</ASP: Treeview>
</Template>
</SharePoint: renderingtemplate>
5. There is an XML file in the project. The file name starts with "fldtypes _". You can describe this custom field type, including the displayed name, assembly information, and display style.
<? XML version = "1.0" encoding = "UTF-8"?>
<Fieldtypes>
<Fieldtype>
<Field name = "typename"> myfield </field>
<Field name = "parenttype"> myfield </field>
<Field name = "typedisplayname"> myfield </field>
<Field name = "typeshortdescription"> myfield </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"> mycustomfieldtypes. myfield, mycustomfieldtypes, version = 1.0.0.0, culture = neutral, publickeytoken = 9f4da001_c38ec5 </field>
</Fieldtype>
</Fieldtypes>
6. The major task is to complete. Put the ascx control and the above XML in the specified folder of SharePoint, register the generated DLL assembly to GAC, and restart IIS. OK! Now we can open our type in Sharepoint. Click Create to view the myfield type.