How To: Create custom content type and list instance in SharePoint 2010 using Visual Studio 2010

There are many blogs and MSDN articles talking about the same topic but I decided to write my own one as I did not get a satisfying explanation of why adding the codes here and there when the articles told me to. I believe what I am trying to achieve here is quite simple and common:

1. Create a content type which is reusable

2. Create a list using the content type created in (1)

3. Changes the default “Title” field’s display name (I will discuss this in a separate post)

Below is the step by step instruction of how to achieve the above tasks and some explanations (to the extends of my knowledge) as why we do it the way described. With Visual Studio 2010, SharePoint developers no longer need to manually create feature and element file, packing the feature into a solution file (wsp) and deploying. All those jobs are now automated by Visual Studio 2010 so we can just focus on our tasks of creating custom content types and list instances.

1. Create a new Visual Studio 2010 project

For the purpose of this demo, we will create a project called “Color” which will hold our color content type and color list using the content type and eventually the feature event receiver will insert some initial list items when the feature was activated.

  1. in VS2010, click [File] –> [New] –> [Project]
  2. In the “Installed Template” pane, select [Visual C#] –> [SharePoint] –> 2010
  3. Make sure [.NET Framework 3.5] is selected as the framework type and click on [List Definition].
  4. Enter “Color” in the Name field, click OK button
    Color01
     
  5. Enter the URL of the site that this solution/feature will be installed (https://SP2010Demo) and select [Deploy as a farm solution] and click Next button.
    Color02
     
  6. Select [Custom List] in the drop down list as the type for the list definition and ensure the checkbox [Add a list instance for this list definition] is check, then click Finish button. You should see something similar in solution explorer to the below screenshot.
    Color04

Note:

  • ListDefinition1/Elements.xml is where you define your custom content types and list templates.
  • ListDefinition1/Schema.xml is where you define the view and fields for that list only and as well as applying the content types that you created in ListDefinition1/Elements.xml.
  • ListDefinition1/ListInstance1/Elements.xml is where you define the list instance and apply list template type.

2. Create a custom content type

We will create a Color content type with 3 fields/columns to store the color's name, RGB value and Hex value. Since we used Custom List as the base type for the list definition, we have Title column by default and there is no need to create one. However we do need to rename “Title” to “Color Name” and we will look into that later.  

  1. Open ListDefinition1/Elements.xml

  2. To declare the fields, insert the following lines in the <Elements> tag and before <ListTemplate> tag. The IDs were generated using GUID  Generator. You can open GUID Generator by going to [Too ls] > [Create GUID] and select format 4.

    <Field ID="{168076D8-500B-4572-B2FA-9CC9C4664F4C}" Type="Text" Name="RGBValue" DisplayName="RGB Value" Required="TRUE" Sealed="TRUE"/>
    <Field ID="{A97BDC8C-8723-455E-818C-56CE8D63BE19}" Type="Text" Name="HexValue" DisplayName="Hex Value" Required="TRUE" Sealed="TRUE"/>
     

  3. To declare the content type, insert the following lines  after the above lines. ugain, Use GUID Generator for ContentType ID. Notice that we are simply attaching the Fields that we created in step 2 above inside <FieldRefs> tag for them to be included in the content type.

    <ContentType ID="0x0100736C62A68F6A408E8D0800B14AF43E0E" Name="Colors" Group="Custom CT" Description="Color content type">
    <FieldRefs>
    <FieldRef ID="{168076D8-500B-4572-B2FA-9CC9C4664F4C}"/>
    <FieldRef ID="{A97BDC8C-8723-455E-818C-56CE8D63BE19}"/>
    </FieldRefs>
    </ContentType>
     

  4. Modify <ListTemplate> tag so it looks like below. The value of “Type” is the identifier for the template and needs to be unique within the feature. Normally I choose numbers larger than 10000 for it. I’ve also added DisallowContentTypes=”False”. See ListTemplate Element (List Template) for more detailed explanations.

    <ListTemplate
            Name="Colors"
            Type="10050"
            BaseType="0"
            OnQuickLaunch="TRUE"
            SecurityBits="11"
            Sequence="410"
            DisallowContentTypes="False"
            DisplayName="Color - ListDefinition1"
            Description="My List Definition"
            Image="/_layouts/images/itgen.png"/>
            

  5. Now your Elements.xml file should look something like below:
    Color05 

3. Apply custom content type in list definition (Schema.xml)

  1. Open ListDefinition1/Schema.xml

  2. Remove the following 2 <ContentTypeRef> tags from <ContentTypes> tag
     
    <ContentTypeRef ID="0x01">
    <FolderTargetName="Item" />
    </ContentTypeRef>
    <ContentTypeRef ID="0x0120" />
     

  3. Add the following <ContentTypeRef> tag into <ContentTypes> tag

    <ContentTypeRef ID="0x0100736C62A68F6A408E8D0800B14AF43E0E"></ContentTypeRef>
     

  4. Here is the tricky part, although we have declared the fields RGBValue and HEXValue in the Color content type already, we still need to declare them in the list definition again. This is somehow SharePoint was designed. Please see Difference between declaring fields in content types and in list definition. Insert the following lines within <Fields> tag. Those are exactly the same lines as in the content type Elements.xml file.

    <Field ID="{168076D8-500B-4572-B2FA-9CC9C4664F4C}" Type="Text" Name="RGBValue" DisplayName="RGB Value" Required="TRUE" Sealed="TRUE"/>
    <Field ID="{A97BDC8C-8723-455E-818C-56CE8D63BE19}" Type="Text" Name="HexValue" DisplayName="Hex Value" Required="TRUE" Sealed="TRUE"/>
     

  5. Add EnableContentTypes="TRUE"  attribute to the <List> tag

  6. Now adding the two fields into list’s default view. Find the <View> tag with BaseViewID=”1'” and change <ViewFields> tag so it looks like

    <ViewFields>
    <FieldRef Name="LinkTitle"></FieldRef>
    <FieldRef Name="RGBValue"></FieldRef>
    <FieldRef Name="HexValue"></FieldRef>
    </ViewFields>
     

  7. Now your Schema.xml file should look like below:
    Color06 

4. Modify ListInstance1/Elements.xml File

  1. Open ListInstance1/Elements.xml file

  2. Modify the <ListInstance> tag so it looks like below

    <?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="https://schemas.microsoft.com/sharepoint/">
    <ListInstance Title="Color"
                    OnQuickLaunch="TRUE"
                    TemplateType="10050"
                    Url="Lists/Colors"
                    Description="A list for colors">
    </ListInstance>
    </Elements>

    Notice that we are telling the list instance to use list template with Type ID 10050.

 

5. Feature Settings

We will change a few feature properties to make it more sensible

  1. Double click on Features/Feature1

  2. Change the Title field to “Color Demo”. This will be the feature name that you see in site collection features page.

  3. Change the Scope drop down list to “Site”. We want to make this feature available at the site collection level instead of the web.

  4. Right click on Features/Feature1 > Properties. In the Properties window, change the Folder Name to “ColorDemoFeature”

  5. At this point, the feature folder name will be “Color_ColorDemoFeature” under the 14 hive. (14\TEMPLATE\FEATURES). To change this, double click on Features/ColorDemoFeature/ColorDemoFeature.feature. In the Properties window, there is a property called “Deployment Path”. By default, it’s set to
    $SharePoint.Project.FileNameWithoutExtension$_$SharePoint.Feature.FileNameWithoutExtension$
    As you can see
    $SharePoint.Project.FileNameWithoutExtension$ = Color
    $SharePoint.Project.FileNameWithoutExtension$ = ColorDemoFeature

    You can play around with this to get your desired feature name sitting in the 14 hive.

 

6. Deployment

This is where the fun starts. Just one click and VS2010 get it done for you!

  1. Double check that we have the correct deployment settings before we start by clicking on the project name Color. In the Properties window, it should have Site URL property set to https://SP2010Demo and Sandboxed Solution set to FALSE.

  2. Right click on the project name Color, then click Deploy. It will go through the following stages to install the solution and features depends on whether it’s the first time you are doing the deployment.

    Build Started …
    Deploy Started …
    Run Pre-Deployment Command
    Recycle IIS Application Pool
    Retract Solution
    Add Solution
    Activate Features
    Run Post-Deployment Command
    Deploy Succeeded

    You can open the Output windows (View > Output) to see more details.

 

References:

  1. You may download the complete Visual Studio 2010 solution file from HERE
  2. See How to change default Title field name in the custom content type
  3. Difference between declaring fields in content types and in list definition
  4. MSDN article about ListTemplate Element (List Template)

Feel free to send me any comment or question.

 

Note:
2011-01-18    Updated the solution file. (Removed Post-deployment command line setting I left in the solution)