Development of Custom Field Types

Source: Internet
Author: User
Tags cdata

 

Development of Custom Field Types-Level 2 combox Linkage

I found some information aboutCustom Field TypeDevelopedArticle. It is found that there are few articles about this part in Moss development. However, this is enough for us to get started.

1. Create a moss2007 Custom Field Type instance

2. Create a moss2007 Custom Field instance with a property value

3, how to: create a custom field type and field control

The first two articles are from meizhai, and the last one is from msdn. It is a good example. I believe there will be a lot of insights after reading it.

Well, I will not talk much about it. Next I will start to develop this 2-level linkage combox.

Create a Sharepoint empty project, and then add a new field control project. After a project is created, the template automatically adds some files for us:

1. citycombox. Field. CS;

2. citycombox. fieldcontrol. CS;

3, fldtypes_citycombox.xml;


You have to add the file yourself:

1. citycomboxvalue. CS;

2, citycontrol. ascx;

3. cityxmlfile. xml;

When creating the citycontrol. ascx file, pay attention to the directory structure, which saves us a little effort during deployment.

After the project is created, we should start writingCode. If you read the three articles mentioned above, you will find that the development of Custom Field Types basically inherits the types provided by Sharepoint, in the source type, rewrite the member method.

1. Type object citycombox. Field

code: using system;
using system. runtime. interopservices;
using system. security. permissions;
using Microsoft. sharePoint;
using Microsoft. sharepoint. webcontrols;
using Microsoft. sharepoint. security;
namespace citycombox
{< br> // todo: replace, as needed, "spfieldtext" with some other class derived from spfield.
// todo: Update, as needed, parenttype element in fldtypes *. XML in this solution.
[clscompliant (false)]
[GUID ("48d7dfb3-a1eb-4c96-af40-0a98b98f021")]
public class citycomboxfield: spfieldmulticolumn
{< br> Public citycomboxfield (spfieldcollection fields, string fieldname)
: Base (fields, fieldname)
{< BR >}

Public citycomboxfield (spfieldcollection fields, string typename, string displayname)
: Base (fields, typename, displayname)
{
}
Public override basefieldcontrol fieldrenderingcontrol
{
[Define pointpermission (securityaction. linkdemand, objectmodel = true)]
Get
{
Basefieldcontrol fieldcontrol = new citycomboxfieldcontrol ();
Fieldcontrol. fieldname = This. internalname;
Return fieldcontrol;
}
}
Public override object getfieldvalue (string value)
{
If (string. isnullorempty (value ))
Return NULL;
Return new citycomboxvalue (value );
}
}
}

 

Note that the blue part inherits spfieldmulticolumn. Because the final result we want to achieve is to record two data in a field, so we inherit the spfieldmulticolumn object.

The red part inherits the spfield, and the green part overrides the member method of spfieldmulticolumn to get our custom control.

The yellow part needs to be added by ourselves. The previous code is automatically generated by the template. Because of this special development method (rewrite), we need to understand the source object to know what to rewrite. It is also necessary to look at the source code.

The type part of the code is completed. The focus is on the control part. Let's look at the control part of the code below.

2. control object citycombox. fieldcontrol

Code:Using system;
Using system. runtime. interopservices;
Using system. Web. UI. webcontrols;
Using system. xml;
Using system. Collections. Generic;

Using Microsoft. SharePoint;
Using Microsoft. Sharepoint. webcontrols;

Namespace citycombox
{
// Todo: replace, as needed, "textfield" with some other class derived from Microsoft. Sharepoint. webcontrols. basefieldcontrol.
[Clscompliant (false)]
[GUID ("34e11e08-29f1-4a8e-8ed2-8800c3c1a5dc")]
Public class citycomboxfieldcontrol: basefieldcontrol
{
Protected dropdownlist ddl_province, ddl_city;

Protected override string defaulttemplatename
{
Get
{
Return "citycomboxfieldrendering ";
}
}

Public override object Value
{
Get
{
Ensurechildcontrols ();
Citycomboxvalue fieldvalue = new citycomboxvalue ();
Fieldvalue. Province = ddl_province.selectedvalue.trim ();
Fieldvalue. City = ddl_city.selectedvalue.trim ();
Return fieldvalue;
}
Set
{
Ensurechildcontrols ();
Citycomboxvalue fieldvalue = (citycomboxvalue) value;
Ddl_province.selectedvalue = fieldvalue. province;
Ddl_province_selectedindexchanged (null, null );
Ddl_city.selectedvalue = fieldvalue. City;
}
}

Public override void focus ()
{
Ensurechildcontrols ();
Ddl_province.focus ();
}

Protected override void createchildcontrols ()
{
If (field = NULL)
Return;

Base. createchildcontrols ();

If (ControlMode = spcontrolmode. Display)
Return;

Ddl_province = (dropdownlist) templatecontainer. findcontrol ("ddl_province ");
If (ddl_province = NULL)
Throw new argumentexception ("specified upted citycomboxfieldrendering template-missing ddl_province .");
Ddl_province.tabindex = tabindex;
Ddl_province.cssclass = cssclass;
Ddl_province.tooltip = field. Title + "Province ";

Ddl_city = (dropdownlist) templatecontainer. findcontrol ("ddl_city ");
If (ddl_city = NULL)
Throw new argumentexception ("specified upted citycomboxfieldrendering template-missing ddl_city .");
Ddl_city.tabindex = tabindex;
Ddl_city.cssclass = cssclass;
Ddl_city.tooltip = field. Title + "city ";

Ddl_province.enabled = false;
Ddl_city.enabled = false;

If (ControlMode = spcontrolmode. New | ControlMode = spcontrolmode. Edit)
{
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (environment. currentdirectory + "\ cityxmlfile. xml ");
Xmlnode rootnode = xmldoc. selectsinglenode ("place ");
Xmlnodelist nodelist = rootnode. childnodes;
Ddl_province.items.clear ();
Foreach (xmlnode node in nodelist)
{
Ddl_province.items.add (New listitem (node. attributes ["name"]. value. tostring (), node. attributes ["name"]. value. tostring ()));
}

If (! Ddl_province.enabled)
{
Ddl_province.enabled = true;
Ddl_province.autopostback = true;
Ddl_province.selectedindexchanged + = new eventhandler (ddl_province_selectedindexchanged );

// Ddl_city.selectedindexchanged + = new eventhandler (ddl_city_selectedindexchanged );
}
If (ddl_province.items.count> 0)
{
// Ddl_province.items [0]. Selected = true;
Ddl_province_selectedindexchanged (null, null );
}
}

}

Private string loadxml (string Province)
{
String result = string. empty;
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (environment. currentdirectory + "\ cityxmlfile. xml ");
Xmlnode rootnode = xmldoc. selectsinglenode ("place ");
Xmlnodelist nodelist = rootnode. childnodes;

Foreach (xmlnode node in nodelist)
{
If (province = "all ")
{
Result = Result + "@" + node. attributes ["name"]. value. tostring ();
}
Else if (node. attributes ["name"]. value. tostring () = Province)
{
Xmlnodelist citylist = node. childnodes;
Foreach (xmlnode city in citylist)
{
Result = Result + "@" + city. attributes ["name"]. value. tostring ();
}
}
}
Return result;
}

Void ddl_province_selectedindexchanged (Object sender, eventargs E)
{
// Throw new exception ("the method or operation is not implemented .");
Ddl_city.enabled = true;
String [] citylist = loadxml (ddl_province.selectedvalue). Split (New char [1] {'@'}, stringsplitoptions. removeemptyentries );
Ddl_city.items.clear ();
For (INT I = 0; I <citylist. length; I ++)
{
Ddl_city.items.add (New listitem (citylist [I], citylist [I]);
}
}

}
}

First, the defaulttemplatename attribute is rewritten. This attribute is used to obtain the template. Therefore, our own template is returned here.

Then, the value attribute is rewritten, And the type object obtains the value on the control through this attribute, or transmits the value to the control.

Finally, the createchildcontrols member method is rewritten. This method is called when controls are needed in Moss. To implement special functions, we need to rewrite this method.

In this example, we want to implement two combox controls: the first one is named data. When you select the data in the first combox control, the city in the second combox control is determined based on the province name in the first combox control and provided to the user.

In the createchildcontrols method, we first made some basic judgments, and then looked for the two controls we needed from our custom template (we used the dropdownlist control here ). Then we initialize the first combox. Here we use the data to generate an XML document cityxmlfile. xml. After initialization of the first combox control, we need to set the control to automatically return, and then bind its selectedindexchanged event to the event processing function ddl_province_selectedindexchanged. In the ddl_province_selectedindexchanged function, we need to bind the second combox according to the data selected in the first combox control.

The code of the control is complete. In this part of the code, we use the citycomboxvalue object.

3. Value Object citycomboxvalue

Code: Using system;
Using system. Collections. Generic;
Using system. text;
Using Microsoft. SharePoint;
Using system. Web;
Namespace citycombox
{
Class citycomboxvalue: spfieldmulticolumnvalue
{
Private const int numberoffields = 2;
Public citycomboxvalue (): Base (numberoffields ){}
Public citycomboxvalue (string value): Base (value ){}
Public String Province
{
Get {return this [0];}
Set {This [0] = value ;}
}
Public String City
{
Get {return this [1];}
Set {This [1] = value ;}
}

}
}

 

Citycomboxvalue inherits from spfieldmulticolumnvalue. To save multiple values in a field, we need to define two attributes.

4. citycontrol. ascx

Code: <% @ Control Language = "C #" classname = "webusercontrol1" %>
<% @ 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 = "citycomboxfieldrendering" runat = "server">
<Template>
<Table>
<Tr>
<TD> Province: </TD>
<TD> <asp: dropdownlist id = "ddl_province" runat = "server">
</ASP: dropdownlist> </TD>
<TD> City: </TD>
<TD> <asp: dropdownlist id = "ddl_city" runat = "server">
</ASP: dropdownlist> </TD>
</Tr>
</Table>
</Template>
</SharePoint: renderingtemplate>

 

Is to create a user control that contains two dropdownlist controls. Finally, let's look at the type description document.

5. type description document fldtypes_citycombox.xml

Code: <? XML version = "1.0" encoding = "UTF-8"?>
<Fieldtypes>
<Fieldtype>
<Field name = "typename"> citycomboxfield </field>
<Field name = "typedisplayname"> citycomboxfield </field>
<Field name = "typeshortdescription"> citycomboxfield </field>
<Field name = "parenttype"> multicolumn </field>
<Field name = "usercreatable"> true </field>
<Field name = "fieldtypeclass"> 48d7dfb3-a1eb-4c96-af40-0a98b98f021 </field>
<Propertyschema>
<Fields>
<Field name = "defaultprovince" displayname = "Default Province:" maxlength = "50" displaysize = "30" type = "text">
<Default> Sichuan </default>
</Field>
<Field name = "defaultcity" displayname = "Default City:" maxlength = "50" displaysize = "30" type = "text">
<Default> Mianyang </default>
</Field>
</Fields>
</Propertyschema>
<Renderpattern name = "displaypattern">
<Switch>
<Expr>
<Column/>
</Expr>
<Case value = "">
</Case>
<Default>
<HTML> <! [CDATA [Province:]> <Column subcolumnnumber = "0" htmlencode = "true"/>
<HTML> <! [CDATA [---- City:]> <Column subcolumnnumber = "1" htmlencode = "true"/>
</Default>
</Switch>
</Renderpattern>
</Fieldtype>
</Fieldtypes>

 

Field tags are basic definitions of field types. For example, <field name = "typeshortdescription"> specifies the name displayed when a column is added; <field name = "parenttype"> multicolumn </field> specifies the presentation type.

<Renderpattern name = "displaypattern">
The display style is determined. It is used to define the style you see.

6. Data document cityxmlfile. xml

Code:<? XML version = "1.0" encoding = "UTF-8"?>
<Place>
<Province name = "Sichuan">
<City name = "Mianyang"> Mianyang </city>
<City name = "Guangyuan"> Guangyuan </city>
<City name = "Deyang"> Deyang </city>
<City name = "Chengdu"> Chengdu </city>
</Province>
<Province name = "Xinjiang">
<City name = "Urumqi"> Urumqi </city>
<City name = "Altay"> Altay </city>
<City name = "Hami"> Hami </city>
<City name = "Shihezi"> Shihezi </city>
</Province>
</Place>

 

Finally, let's take a look at the final effect.



 

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.