Create Custom List Definitions in SharePoint 2010
Getting Started with Web Development in SharePoint 2010: Learn how to create a custom list definition in SharePoint 2010 and then attach an event receiver to it.
Applies to: SharePoint Foundation 2010 | SharePoint Server 2010 | Visual Studio | Visual Studio 2010
Published: June 2010
Provided by: Frank Rice, Microsoft Corporation
In this exercise, you create a custom list definition in Microsoft SharePoint 2010 and then create an event receiver that is triggered when the list is used. To complete this task, you must do the following:
Create a SharePoint List Definition Project
Customize the SharePoint List Definition
Test the Solution
Create a SharePoint List Definition Project
In this task, you create a list definition SharePoint 2010 project in Microsoft Visual Studio 2010.
To create the SharePoint project
To start Visual Studio 2010, click the Start Menu, click All Programs, click Microsoft Visual Studio 2010, and then click Microsoft Visual Studio 2010.
On the File menu, point to New, and then click Project.
In the New Project dialog window, in the Installed Templates section, click Visual C#, click SharePoint, and then click 2010.
Click List Definition from the project items.
In the Name box, type Bugs and then click OK.
In the SharePoint Customization Wizard, type the local Web site that you want to use for this exercise (such as https://localhost/SampleWebSite).
For the trust level, select Deploy as a farm solution and then click Next.
In the What is the display name of the list definition? Box, type Bugs.
In the What is the type of the list definition? drop-down list, select Custom List.
Select the Add a list instance for this list definition check box and then click Finish.
Customize the SharePoint List Definition
This task guides you through customizing the List Definition project template by changing the list name, the content type it stores, and the fields that are displayed in the new form, edit form, and display form.
To customize the list definition
In Solution Explorer, expand ListInstance1 and open the Elements.xml file.
Within the ListInstance element change the Title attribute to Bugs and change the TemplateType attribute to 10001.
In Solution Explorer, before Schema.xml, open Elements.xml.
Within the ListTemplate element, change the Type attribute to 10001 and add the DisallowContentTypes=”FALSE” attribute.
Insert the following XML into the top of the Elements element in the Elements.xml file as shown in Figure 1. This XML describes the Bug Item content type the list stores.
<!-- Describes content types. --> <ContentType ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" Name="Bug Item" Group="Custom Content Types" Description="Bug item content type." Version="0"> <FieldRefs> <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" /> <FieldRef ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" /> <FieldRef ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" /> <FieldRef ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" /> </FieldRefs> </ContentType> <!-- End describes content types. -->
Insert the following XML into the top of the Elements element in the Elements.xml file as shown in Figure 1. This XML describes the fields the Bug Item content type uses.
<!-- Describes fields in bug list. --> <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" /> <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" /> <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" Group="Custom Columns" /> <!-- End describes fields in bug list. -->
Figure 1. Elements.xml file after inserting two code sections
In Solution Explorer, open Schema.xml.
Add the EnableContentTypes=”TRUE” attribute to the List element inside the Schema.xml file.
Insert the following XML into the ContentTypes element in the schema.xml file as shown in Figure 2. This XML describes the Bug Item content type this list stores.
<!-- Start Add. --> <ContentTypeRef ID="0x010089E3E6DB8C9B4B3FBB980447E313CE94" /> <!-- End Add. -->
Insert the following XML into the Fields element as shown in Figure 2. This XML describes the fields the list stores. These are directly related to the fields in the content type that you added in the previous step.
<!-- Start Add. --> <Field Type="Note" DisplayName="Description" Required="FALSE" NumLines="6" RichText="FALSE" Sortable="FALSE" ID="{cb55bba1-81a9-47b6-8e6c-6a7da1d25602}" StaticName="BugDescription" Name="BugDescription" Group="Custom Columns" /> <Field Type="Text" DisplayName="Project" Required="FALSE" MaxLength="255" ID="{0248c82f-9136-4b3a-b802-d0b77280b3bc}" StaticName="BugProject" Name="BugProject" Group="Custom Columns" /> <Field Type="Text" DisplayName="Assigned To" Required="FALSE" MaxLength="255" ID="{aa4a82dd-5b32-4507-9874-4e1c7bca3279}" StaticName="BugAssignedTo" Name="BugAssignedTo" -Group="Custom Columns" /> <!-- End Add. -->
Figure 2. Schema.xml after inserting the ContentTypeRefs code and Fields code
Insert the following XML into the ViewFields element in the second view, where BaseViewID="1" as shown in Figure 3. This XML describes which fields should be visible in this particular view. BaseView 1 is set as the default view. This is the view the newly created fields should display in.
<!-- Start Add. --> <FieldRef Name="BugDescription"></FieldRef> <FieldRef Name="BugProject"></FieldRef> <FieldRef Name="BugAssignedTo"></FieldRef> <!-- End Add. -->
Figure 3. Schema.xml after Fields XML insert
In Solution Explorer, right-click the Bugs node, and then click Deploy.
Test the Solution
In this task, you test the solution by adding a bug to the Bugs list.
To add an item to the Bugs list
Open the website that you specified previously.
On the Home page, click the Bugs list in the left navigation pane.
On the List Tools tab, click Items, and then click Bug Item on the New Item drop-down list as shown in Figure 4.
Figure 4. Bug Item from the New Item drop-down list
The Bugs - New Item screen is displayed as shown in Figure 5.
Figure 5. New Bug Item dialog box
In the Bugs - New Item screen, type Bug01 in the Title box.
Type Who is going to track this bug? in the Description box, type Writing developer articles in the Project box, and then click Save.