Walkthrough: Create a Site Column, Content Type, and List for SharePoint

The following procedures demonstrate how to create custom SharePoint site columns—or fields—as well as a content type that uses the site columns. It also shows how to create a list that uses the new content type.

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Personalizing the Visual Studio IDE.

This example creates a list for managing patients in a hospital. First, you must create a SharePoint project in Visual Studio and add site columns to it, as follows.

To create the project

On the Visual StudioFile menu, choose New, Project.

In the New Project dialog box, under either Visual C# or Visual Basic, expand the SharePoint node, and then choose 2010.

In the Templates pane, choose SharePoint 2010 Project, change the name of the project to Clinic, and then choose the OK button.

The SharePoint 2010 Project template is an empty project that's used in this example to contain site columns and other project items that are added later.

On the Specify the site and security level for debugging page, enter the URL for the local SharePoint site to which you want to add the new custom field item, or use the default location (http://<SystemName>/).

In the What is the trust level for this SharePoint solution? section, use the default value Deploy as a sandboxed solution.

Choose the Finish button. The project should now be listed in Solution Explorer.

To add site columns

Add a new site column. To do this, in Solution Explorer, open the shortcut menu for Clinic, and then choose Add, New Item.

In the Add New Item dialog box, choose Site Column, change the name to Patient Name, and then choose the Add button.

In the site column's Elements.xml file, leave the Type setting as Text, and change the Group setting to Clinic Site Columns. When complete, the site column's Elements.xml file should look like the following example.

Next, create a content type—based on the Contacts content type—that includes the site columns that you created in the previous procedure. By basing a content type on an existing content type, you can save time because the base content type provides several site columns for use in the new content type.

To create a custom content type

Add a content type to the project. To do this, in Solution Explorer, choose the project node

On the menu bar, choose Project, Add New Item.

Under either Visual C# or Visual Basic, expand the SharePoint node, and then choose the 2010 node.

In the Templates pane, choose the Content Type template, change the name to Patient Info, and then choose the Add button.

The SharePoint Customization Wizard opens.

In the Which base content type should this content type inherit from list, choose Contact as the content type on which to base the new content type, and then choose the Finish button.

Doing this gives you access to other potentially useful site columns in the Contact content type, in addition to the site columns that you defined previously.

After the Content Type designer appears, in the Columns tab, add the three site columns that you defined previously: Patient Name, Patient ID, and Doctor Name. To add these columns, choose the first list box in the site columns list under Display Name, and then choose each site column in the list one at a time.

Tip

To choose the site columns more quickly, filter the list by entering the first few letters of the name of the column.

In addition to the three custom site columns, add the Comments site column from the site columns list.

Select the Required check box for the Patient Name and Patient ID site columns to make them required fields.

On the Content Type tab, make sure that the content type name is Patient Info, and then change the description to Patient information card.

Change Group Name to Clinic Content Types, and leave the other settings at their default values.

On the menu bar, choose File, Save All, and then close the Content Type designer.

Choose the new row, choose the Patient Info content type in the list of content types, and then choose the OK button.

Doing this adds all of the site columns from the Patient Info content type into the list.

Delete all of the site columns in the list except for the following:

Patient ID

Patient Name

Home Phone

E-Mail

Doctor Name

Comments

Under Column Display Name, choose an empty row, add a custom list column, and name it Hospital. Leave its data type as Single Line of Text.

The custom list column applies only to this list. When you add a custom list column to a list, a new list content type, including all the columns added into the list, is created and set as the default list.

Tip

If you choose a column from the list of site columns, an existing site column is used. However, if you enter a column name value without choosing any columns in the list, a custom list column is created, even if a column with the same name already exists in the list.

Optionally, rather than setting the data type for the custom list column to Single Line of Text, you could instead set the data type for this column to Lookup, and its values would be retrieved from a table or another list. For information about Lookup columns, see List Relationships in SharePoint 2010 and Lookups and List Relationships.

Next to the Patient ID and Patient Name boxes, select the Required check box.

On the Views tab, choose an empty row to create a view. Enter Patient Details.

On the Views tab, you can specify the columns that you want to appear in the SharePoint list.

Choose the new Patient Details row, and then choose the Set as Default button.

The new view is now the default view for the list.

Add the following columns to the Selected Columns list in the following order:

Patient ID

Patient Name

Home Phone

E-Mail

Doctor Name

Hospital

Comments

In the Properties list, choose the Sorting and Grouping property, and then choose the ellipsis button to display the Sorting and Grouping dialog box.

In the Column Name list, choose Patient Name, make sure that the Sorting column is set to Ascending, and then choose the OK button.