Changing the Structure of the List
One of the beautiful things about a list definition is that you can define a rich set of predefined lists that already contains the fields and views that you need to solve a problem. Most of the list formats that SharePoint offers, with the notable exception of the issues list, are little more than a custom list defined with a set of predefined fields that make sense for a specific purpose. The contacts list is, as the name suggests, good for maintaining a list of contacts. The links list is good for capturing links, the tasks list is good for capturing tasks, and so on.
For this article's purposes, you're going to further modify the contacts list to add a new field. This new field will be designed to capture the type of the contact in the contact list. For instance, the contact can be: an acquaintance, a friend, a colleague, a competitor, a vendor, a customer, or some other kind of contact. Before you can add this field to the list definition, you have to create the CAML fragment that represents the field.
Because this is a choice field, the values are a bit more complicated. The basic format for a field is a single <Field> node. A choice field has the additional sub-nodes of <Default> and <CHOICES>. The <CHOICES> node further has one or more <CHOICE> nodes indicating the individual choices for the field.
The three basic attributes required for a <Field> node are:
- Name: This is the InternalName used in SharePoint. It must be unique. The name is only seen by developers working on code that works with the list.
- Display Name: This is the name that users see for the field. It is displayed in data entry screens and as the header for views of the list.
- Type: This is the fundamental type of the field. For instance, Text, Note, URL, and Choice are all different fundamental types of fields.
When using a choice field, in addition to these attributes, you'll also need the
Format attribute to indicate the form of choice (
DropDown), and the
FillInChoice attribute which indicates whether the user can type in a response. Putting this all together, the CAML fragment for the field is:
<Field Type="Choice" DisplayName="TypeOfContact" Format="Dropdown" FillInChoice="FALSE" Name="TypeOfContact">
<Default>Acquaintance</Default>
<CHOICES>
<CHOICE>Acquaintance</CHOICE>
<CHOICE>Friend</CHOICE>
<CHOICE>Colleague</CHOICE>
<CHOICE>Competitor</CHOICE>
<CHOICE>Vendor</CHOICE>
<CHOICE>Customer</CHOICE>
<CHOICE>Other</CHOICE>
</CHOICES>
</Field>
With the CAML fragment in hand, you can add the field to the list definition. The following steps show you how to add a field to a list definition:
- Open the SCHEMA.XML.
- Locate the List\MetaData\Fields node.
- Before the closing tag of the <Fields> node, add the new <Field> node for the field that you want to add.
- Save the file.
- Perform an IISRESET.
- Create a new list from the list definition and verify that the field is present.
A Flexible Framework for Customization
SharePoint provides a extremely flexible framework for customizing the way that your lists look and work. You can change the structure of the list pages much like you can change the structure of the main Web part pages in a site definition. You can also control the Web parts on the page just as you can in a site definition. Further, you can control the types of fields that are created when the list is created. This means that you can develop nearly every kind of list that you can dream up.