Share via


Walkthrough: Create a Custom Field, Content Type, List Definition, and List Instance

The following procedure illustrates how to create a custom field in SharePoint called Patient Name, as well as a content type that uses the field. It also demonstrates how to add a list definition to the project, along with a list instance based on the list definition, that uses the new content type and field.

This walkthrough demonstrates the following tasks:

  • Creating and customizing a field.

  • Creating and customizing a content type that uses the field.

  • Creating and customizing a list definition.

  • Creating and customizing a list instance of the list definition.

  • Viewing the new field in the content type and list instance.

Note

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 Visual Studio Settings.

Prerequisites

You need the following components to complete this walkthrough:

Creating a Custom Field Item

First, create a custom field item as a SharePoint project in Visual Studio called Patient Name.

To create a custom field item

  1. Display the New Project dialog box by pointing to New on the File menu, and then clicking New Project.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. Click Empty SharePoint Project in the Templates pane, change the name of the project to CustomField1, and then click OK.

    The SharePoint Customization Wizard appears. The Empty Project template is used for custom field items because there is no template for them.

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

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

    For more information about sandboxed and farm solutions, see Sandboxed Solution Considerations.

  6. Click Finish. You should see the project appear in Solution Explorer.

  7. Add an Empty Element project item to the project. To do this, click the field's project node (CustomField1) in Solution Explorer and then click Add New Item on the Project menu.

  8. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  9. In the Templates pane, select Empty Element, keep the default name EmptyElement1, and then click Add.

  10. In the Elements.xml file under EmptyElement1, add the following after the <Elements> tag:

    <Field ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" 
        Type="Note" 
        Name="PatientName" 
        DisplayName="Patient Name" 
        Group="A Custom Group">
    </Field>
    

    This adds a field that is named Patient Name.

    Important

    When adding a custom field, you must specify the ID, Type, Name, DisplayName, and Group attributes for the field to appear correctly in SharePoint. Statement completion erroneously indicates that you need only the first three of these attributes when in fact all five are required.

  11. Press F5 to run and deploy the project to SharePoint.

    If you get a message that script debugging is disabled, click Yes to continue debugging.

  12. If this is the first time you have debugged a Web application in Visual Studio, you will be asked whether you want to modify the Web configuration file to enable debugging. Answer Yes.

  13. At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.

  14. Under the Galleries section of the Site Settings page, click the Site columns link.

  15. In the Site Column Gallery page, scroll the page down to the Custom Columns heading and note the new field item added, Patient Name.

  16. Close the browser.

Creating a Custom Content Type

Next, create a content type, based on the SharePoint Contacts list, that uses the new field created in the previous procedure.

To create a custom content type

  1. Add a content type to the project. To do this, click the project node in Solution Explorer and then click Add New Item on the Project menu.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. In the Templates pane, select Content Type and use the default name, ContentType1.

  4. In the SharePoint Customization Wizard, select Contact as the content type on which to base the new one, and then click Finish.

    This opens the content type's Elements.xml file for editing.

  5. In the <FieldRefs> section of Elements.xml, copy and paste in:

    <FieldRef ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" 
        Name="PatientName" 
        DisplayName="Patient Name" 
        Required="TRUE"/>
    

    The FieldRef ID used here refers to the Field ID we created for the custom field.

  6. Press F5 to run and deploy the project.

  7. At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.

  8. Under the Galleries section of the Site Settings page, click the Site content types link.

  9. In the Site Content Types Gallery page, note the new content type we just created, CustomField1 - ContentType1. Click it to view its fields. In the list of fields for CustomField1 - ContentType1, note the field Patient Name is added.

  10. Close the browser.

Creating a List Definition and List Instance

Now, create a list definition and list instance that will use the new content type and field. Because we want to create the list definition based on the content type we just created, we will use the List Definition From Content Type project item template.

To create a list definition and list instance

  1. Add a list definition to the project. To do this, click the project node in Solution Explorer and then click Add New Item on the Project menu.

  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.

  3. In the Templates pane, select List Definition From Content Type and use the default name, ListDefinition1.

  4. In the What is the display name of the list definition? box, leave the default value, CustomField1 - ListDefinition1.

  5. In the What content type would you like to use for your list definition? list, use the default (and only) value, CustomField1 - ContentType1.

    This creates a customizable list definition based on the new, modified Contacts list.

  6. Select the Add a list instance for this list definition box to add a list instance project item to the project. The list instance will be an instance of the new list definition.

  7. Click Finish. You should see the list definition appear in Solution Explorer.

Customizing the List Definition

Finally, customize the list definition by adding a new field to it, Patient Name.

Important

The value of the Name attribute in the list definition's Elements.xml must match the folder name of the list definition project item or an error will occur when the project is run. To make sure that the Name attribute matches the folder name, the replacement parameter (or token) $ProjectItemName$ is used. When the SharePoint project is packaged and deployed, this token is converted into the actual folder name of the list definition. If you change the value of the Name attribute to something other than this token, then your application may not run correctly. For more information, see Replaceable Parameters.

To add a field to the list definition

  1. In Solution Explorer, double-click Schema.xml under ListDefinition1 to view it.

    Schema.xml defines all of the fields and other elements of the list definition, such as name and address. Notice under the <Fields> element at the top of the file that the following field definition was added by the content type:

    <FieldRef ID="{5744d18c-305e-4632-8bd1-09d134f4830d}" 
        Name="PatientName" 
        DisplayName="Patient Name" 
        Required="TRUE" />
    
  2. Press F5 to run and deploy the project.

  3. Under the Lists section in the SharePoint QuickLaunch bar, click the CustomField1 - ListInstance1 link.

    This is the list instance for the new list definition. Note that the new Patient Name field does not yet appear as a column in the list instance.

  4. Click List in the List Tools menu tab at the top of the page and then click the Modify View button on the ribbon bar.

  5. In the list of available Column Names, select Patient Name and then click OK. Notice that the Patient Name field now appears in the list instance.

See Also

Other Resources

Developing SharePoint Solutions

How to: Create a Custom Field Type

Content Types

Columns