How to: Create a Custom Content Type with Event Receivers

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This topic demonstrates how to create a custom content type in Visual Studio using Visual Studio extensions for Windows SharePoint Services.

To create a custom content type

  1. In Visual Studio, click the File menu, point to New, and then click Project.
  2. Under SharePoint, click the Empty project template.
  3. In Visual Studio, right-click the project name, point to Add, and then click New Item.
  4. In the Categories pane, click SharePoint. In the Templates pane, click Content Type, enter the content type name (for example, the name of the content type for a training course is TrainingCourseContentType) and then click Add.
  5. In the Content Type Settings drop-down box, click a base content type (for this procedure, use the Item base content type), select the Add with Event Receiver check box, and then click OK.
  6. Visual Studio generates a code file for the list item event receiver and list event receiver and the Elements XML file, which is part of the feature that will define the content type. Open the XML file.
  7. Change the Name and Description attributes to something more relevant than the default settings. For example, the Name attribute of the training course content type is TrainingCourseContentType and the Description attribute is Training Course Content Type.
  8. The generated content type XML file includes a template for custom fields. Uncomment the <Field> element and provide a new name, display name, and type. The StaticName attribute should be deleted as this attribute is not used. The following is the definition for the TrainingCourseEnrollmentDate field.
    <Field ID="{43568365-8448-4130-831C-98C074B61E89}" Type="DateTime" Name="TrainingCourseEnrollmentDate" DisplayName="Enrollment Deadline" Hidden="FALSE" Required="TRUE" Sealed="FALSE" Format="DateOnly" />
    
    
  9. Copy and paste the field definition from step 8 for each of your custom fields. For each field you create, you must generate a new GUID for the ID attribute. To do this, click Create GUID on the Tools menu, click Registry Format, click Copy, and then click Exit.
  10. There is a single field reference included in the file. Field references correspond to the columns in the list view. For example, Figure 1 has six columns and six field references. Change the name of the field reference to something more relevant than the default setting. For example, the name of one of the field references for the training course content type is TrainingCourseCode.

    Ff647209.new_course_listed(en-us,PandP.10).png

    Figure 1
    Example of field references and columns

  11. There should be a FieldRef ID for each column that appears on the list view. To add additional field references, go to the FieldRefs section and add a new field reference line. The following is an example.
    <FieldRef ID="" Name="TrainingCourseDescription"/>
    
    
  12. For each FieldRef, the FieldRef ID must be an ID of one of your custom fields or an ID of an existing field.
  13. Visual Studio extensions for Windows SharePoint Services will generate stubs for your list item and list event handlers. To implement the ItemAdded event handler, uncomment the following lines.
    public override void ItemAdded(SPItemEventProperties properties)
    {
    }
    
    
  14. Implement your business logic.
  15. Visual Studio extensions for Windows SharePoint Services automatically adds the Receivers element to the Content Type XML file the next time that you refresh the WSP view (to do this, point to Other Windows on the View menu, and then click WSP View) or deploy the solution (to do this, right-click the project, and then click Deploy).

To verify the deployment of the content type, browse the site where the content type was deployed. Browse to the Site Settings page (to do this, click the Site Actions tab, and then click Site Settings), and click Site Content Types under Galleries.

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Show: