Content Types and Custom Fields in SharePoint 2010

In this article we will create a custom content type which we will use to display the order information from AdventureWorks database in following articles. Order content type will include fields such as order details, customer information, shipping address, and more. Also, we will create a template list of orders for the convenience of its reuse.

Download source code

Prerequisites

To get started, we need:

  • Microsoft Visual Studio 2010
  • Microsoft SQL Server 2012
  • AdventureWorks 2012 database
  • Microsoft SharePoint Server 2010 are installed and configured. Expected to have at least one SharePoint site. In my case, the site has a URL: http://intel-w7/

Creating a project for the types of content

Start the Microsoft Visual Studio. Create a Blank Solution named A02.sln. Do File-> Add-> New Project …

In the New Project dialog, navigate to the list of Installed Templates section of SharePoint-> 2010. Select the template Empty SharePoint Project. Set the project name A02.ContentTypes. Click OK.

SharePont Customization Wizard opens, which will be asked to choose the level of trust to be created SharePoint solution. Select Deploy as a farm solution. Also here you can specify the URL of the site SharePoint, which will be created to deploy and debug solutions. In my case, it is a site http://intel-w7/. Click Finish.

Adding a new content type

To run the project A02.ContentTypes Add-> New Item … In the Add New Item dialog box, select the Content Type template and give it a name A02_OrderContentType. Click Add.

In the dialog box will be prompted to select the base type of content from which to derive our first new type of content. Select the type of Item. Click Finish.

You can see that the contents of the project in the Features folder added a new feature called Feature1.feature, and also added a new content type A02_OrderContentType. Double-click on an item A02_OrderContentType. Opens its child file Elements.xml.

Find a ContentType element and make the following changes:

  • Change the value of the Name attribute on the “[A02] Order”
  • Change the value of the Group attribute on the  “[A02] Content Types”
  • Change the value of the Description attribute on  the “Order Information Content Type”

Rename Feature1.feature feature in A02_ContentTypes.feature.
To deploy the solution on the server SharePoint, in a project context menu, select Deploy.
Open a browser and test your SharePoint-site. Select Site Actions-> Site Settings.

Find the Galleries section and select Site content types

In the list of content types, site search group [A02] Content Types and below the content type [A02] Order. Click on the type of content.

Note that the properties Name, Description, Group have values corresponding to changes made to the Elements.xml.

Creating a project for the types of fields

Now, create a separate project for the types of fields. Do File-> Add-> New Project … Select the template Empty SharePoint Project. Give the project a name A02.FieldsTypes.

Add a new description of the type of field Order Details

To create a new type of field, you need to create a class that inherits from a class or series of SPField its derived classes, and then add the XML-file describing the new field.

To begin, add a new class to the project OrderDetailsField. While, for simplicity, our field of Information Order will have text type (ie, to derive a class SPFieldText). Open the file OrderDetailsField.cs and enter the code
using Microsoft.SharePoint;
namespace A02.Fields
{
class OrderDetailsField : SPFieldText
{
public OrderDetailsField(SPFieldCollection fields, string fName)
: base(fields, fName) { }
public OrderDetailsField(SPFieldCollection fields, string tName, string dName)
: base(fields, tName, dName) { }
}
}

Next, you need to create a XML-file fldtypes *. Xml describing a new type of field. This XML file will be deployed in a special folder “%Program Files%\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\XML”, which contains among other things, types definitions of fields in SharePoint. In the context menu of A02.Fields project select Add->SharePoint Mapped Folder… Select a folder TEMPLATE\XML. Click OK.

The folder is added to the XML project. Now run the shortcut menu for that folder Add-> New Item … Select the template XML File. In accordance with the recommendations of Microsoft’s naming of XML files with the types of fields, name the file fldtypes_OrderDetails_A02.xml. Open the file and enter text

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">A02.OrderDetails</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Order Details</Field>
<Field Name="TypeShortDescription">[A02] Order Details</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">A02.Fields.OrderDetailsField, $SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>

Unfortunately, the file fldtypes *. Xml has no XSD-schema, so the syntax can be checked only at the documentation on MSDN (http://msdn.microsoft.com/en-us/library/ff407271).

Adding a new site column Order Details

So, we have added a description of the type of field Order Details. This kind of template to create the columns (columns), which in SharePoint can have two types: Column Site (Site column) and the column list (List column). The difference between them is that the site column is defined once and can then be reused in a variety of lists, while the column list is created directly in the list, in which it will be used.
We will add a new column to the site (Site column). In the project context menu A02.ContentTypes do Add-> New Item… Select an item template Empty Element and give it a name A02_Columns. Click Add.

The new element is added to the project A02_Columns. Open it with the child file Elements.xml and enter the following code

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<!-- Order Details Field  -->
<Field ID="{C293CEAE-0E52-41E2-A24E-4CC83F7695E1}"
Name="A02.OrderDetails"
DisplayName="[A02] Order Details"
Type="A02.OrderDetails"
Sealed="TRUE"
Required="TRUE"
Group="[A02] Columns">
</Field>
</Elements>

Thus, we have identified a new site column of type A02.OrderDetails.
It should be noted that the documentation for SharePoint Column Field, and the terms are interchangeable. Column The term most often used to describe the field as part of a list item (ie, column).

Adding a link on the Order Details in the content type

So, we created a box Order Details, and we want to make it an integral part of the content type Order.
Open the file content type Elements.xml A02_OrderContentType ContentType site and add the following code

<FieldRefs>
<FieldRef ID="{C293CEAE-0E52-41E2-A24E-4CC83F7695E1}" Name="A02.OrderDetails" DisplayName="Order Details" Required="TRUE"/>
</FieldRefs>

Thus, we have added a link on the Order Details in the content type Order.

See results

Deploy the solution within SharePoint. Go to the test site. Follow the Site Actions-> Site Settings. In the Galleries section click Site columns

Our site column [A02] Order Details added to the Site Columns gallery

Return to the Site Settings, select the Galleries section, click Site content types and select the type of content [A02] Order. Please note that  Order Details column now has been added to the Columns list.

Adding Customer Information and Address Bill To fields types definitions

Add classes OrderCustomerInformationField и OrderAddressBillToField to the project.
The content of the file OrderCustomerInformationField.cs:

using Microsoft.SharePoint;
namespace A02.Fields
{
class OrderCustomerInformationField : SPFieldText
{
public OrderCustomerInformationField(SPFieldCollection fields, string fName)
: base(fields, fName) { }
public OrderCustomerInformationField(SPFieldCollection fields, string tName, string dName)
: base(fields, tName, dName) { }
}
}

The content of the file OrderAddressBillToField.cs:

using Microsoft.SharePoint;
namespace A02.Fields
{
class OrderAddressBillToField : SPFieldText
{
public OrderAddressBillToField(SPFieldCollection fields, string fName)
: base(fields, fName) { }
public OrderAddressBillToField(SPFieldCollection fields, string tName, string dName)
: base(fields, tName, dName) { }
}
}

Similarly, add Customer Information and Address Bill To field type definitions.
The content of the file fldtypes_OrderCustomerInformation_A02.xml:

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">A02.CustomerInformation</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Customer Information</Field>
<Field Name="TypeShortDescription">[A02] Customer Information</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">A02.Fields.CustomerInformationField, $SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>

The content of the file fldtypes_OrderAddressBillTo_A02.xml:

<?xml version="1.0" encoding="utf-8" ?>
<FieldTypes>
<FieldType>
<Field Name="TypeName">A02.OrderAddressBillTo</Field>
<Field Name="ParentType">Text</Field>
<Field Name="TypeDisplayName">Order Address Bill To</Field>
<Field Name="TypeShortDescription">[A02] Order Address Bill To</Field>
<Field Name="UserCreatable">TRUE</Field>
<Field Name="FieldTypeClass">A02.Fields.OrderAddressBillToField, $SharePoint.Project.AssemblyFullName$</Field>
</FieldType>
</FieldTypes>

Adding Order Customer Information and Order Address Bill To site columns

In A02.ContentTypes project open child file Elements.xml of A02_Columns item and add the following code to the node Elements:

<!-- Order Customer Information Field  -->
<Field ID="{2086A2BE-1AF8-45C3-8202-99A388165393}"
Name="A02.OrderCustomerInformation"
DisplayName="[A02] Order Customer Information"
Type="A02.OrderCustomerInformation"
Sealed="TRUE"
Required="TRUE"
Group="[A02] Columns">
</Field>


<!-- Order Address Bill To Field  -->
<Field ID="{082C88D1-6C46-4D71-B9A7-6B86D4E08AAB}"
Name="A02.OrderAddressBillTo"
DisplayName="[A02] Order Address Bill To"
Type="A02.OrderAddressBillTo"
Sealed="TRUE"
Required="TRUE"
Group="[A02] Columns">
</Field>

Adding links to the new site columns, content type Order

Open the child file Elements.xml of A02_OrderContentType item and add the following code to the node FieldRefs:

<FieldRef ID="{2086A2BE-1AF8-45C3-8202-99A388165393}" Name="A02.OrderCustomerInformation" DisplayName="Customer Information" Required="TRUE"/>
<FieldRef ID="{082C88D1-6C46-4D71-B9A7-6B86D4E08AAB}" Name="A02.OrderAddressBillTo" DisplayName="Address Bill To" Required="TRUE"/>

Creating list definition

For ease of reuse, we will create orders list definition from the content type [A02] Order. In the project context menu execute A02.ContentTypes Add-> New Item … Select a template of a new element: List Definition from Content Type. Assign name of new list definition as A02_OrdersListDefinition. Click Add.

SharePoint Customization Wizard opens, where you will be asked to select the type of content to create a list definition and display name of the list definition. Enter the name of the list definition as [A02] Orders List. Select the content type [A02] Order (A02.ContentTypes \ A02_OrderContentType). Turn off the option Add a list instance for this list definition. Click Finish.

Open the child file Elements.xml of A02_OrdersListDefinition item and in ListTemplate node replace the Description attribute value by the “[A02] Orders List Definition”.
Click the child element Schema.xml file of A02_OrdersListDefinition item.
Find the Fields node. For each Field node remove the prefix “[A02]” in value of DisplayName attribute. Should get the following code:

<Fields>
<Field ID="{c293ceae-0e52-41e2-a24e-4cc83f7695e1}" Name="A02.OrderDetails" DisplayName="Order Details" Type="A02.OrderDetails" Sealed="TRUE" Required="TRUE" Group="[A02] Columns"></Field>
<Field ID="{2086a2be-1af8-45c3-8202-99a388165393}" Name="A02.OrderCustomerInformation" DisplayName="Order Customer Information" Type="A02.OrderCustomerInformation" Sealed="TRUE" Required="TRUE" Group="[A02] Columns"></Field>
<Field ID="{082c88d1-6c46-4d71-b9a7-6b86d4e08aab}" Name="A02.OrderAddressBillTo" DisplayName="Order Address Bill To" Type="A02.OrderAddressBillTo" Sealed="TRUE" Required="TRUE" Group="[A02] Columns"></Field>
</Fields>

Find the Views node. This node contains definitions of views of list for regular and mobile versions of the site. Go to the  View sub node, which has an attribute MobileView = “TRUE”. Find the ViewFields node. Add the following code in it:

<FieldRef Name="A02.OrderDetails"></FieldRef>
<FieldRef Name="A02.OrderCustomerInformation"></FieldRef>
<FieldRef Name="A02.OrderAddressBillTo"></FieldRef>

For the second View node, also add the code mentioned above and delete a reference to the Attachments field:

<FieldRef Name="Attachments"></FieldRef>

See results

Deploy the solution within SharePoint environment. Go to the test site. Follow the Site Actions-> Site Settings. In the Galleries section click Site columns and make sure that now the [A02] Columns group contains three columns: [A02] Order Address Bill To, [A02] Order Customer Information, [A02] Order Details. Now open the Site Content Types list, and then select our [A02] Order content type  in the [A02] Content Types group. The list of columns also should contain our ones: [A02] Order Address Bill To, [A02] Order Customer Information, [A02] Order Details. Now in the left-hand menu select Lists.

Click Create.

Create dialog opens. Note that the description of our [A02] Orders List presents in the list of available types. Select [A02] Orders List. Enter the name of the list Orders List. Click Create.

The link to a Orders List should be added in the left-hand menu. Open it. As you can see, the list view, in accordance with our changes, displays the fields: Title, Order Details, Order Customer Information, Order Address Bill To.

Conclusion

So, in this article we created a several field types definitions, we learned how to create site columns and how to set field references in the content type. Finally, we created list definition from the Order content type, and later we created a list of orders with the required number of columns from that list definition. You may notice that added site columns don’t do anything useful, and they use default functionality of a simple text columns. We will fix it in the next article,in which we will discuss the creation of Rendering Controls for Custom Fields in SharePoint.

Download source code

Leave a comment