Sunday, October 17, 2010

How to: Designing List schemas, instances and content types in SharePoint 2010


Hi all, 

In this walk through I will step-by-step to develop the a custom list definition, list instance and custom content type.

Step1: Create a new VS 2010 project. New >> Project >> Visual C# >> SharePoint 2010 >> Empty SharePoint Project. 

Project name: Lab04_ListsAndSchemas
.NET Framework 3.5 



Step2: Right click on the project >> Add >> New Item >> Choose Content type.
Name: ProductCT. 



Step3: We create two site columns (fields, site wide) as follows:
Name: Product Description , Product ID
Type: Text, Number 

In the content type, we add field reference as well. 

Elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <!-- Site Columns -->
  <Field SourceID="http://schemas.microsoft.com/sharepoint/v3"
         ID="{B39F9441-0124-4221-9C64-D61983A3397A}"
         Name="ProductDescription"
         DisplayName="Product Description"
         Group="My Custom Columns"
         Type="Text"
         DisplaceOnUpgrade="TRUE" />

  <Field SourceID="http://schemas.microsoft.com/sharepoint/v3"
         ID="{22A5A133-BC36-4AD4-93EA-A5DE9C36F417}"
         Name="ProductID"
         DisplayName="Product ID"
         Group="My Custom Columns"
         Type="Number"
         DisplaceOnUpgrade="TRUE" />

  <!-- Parent ContentType: Item (0x01) -->
  <ContentType ID="0x01003044473278df426692f1919852d4d8af"
               Name="Lab04_ListsAndSchemas - ProductCT"
               Group="Custom Content Types"
               Description="My Content Type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
      <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}"
          Name="Title"
          DisplayName="Product Name" />
      <FieldRef ID="{B39F9441-0124-4221-9C64-D61983A3397A}"
                Name="ProductDescription" />
      <FieldRef ID="{22A5A133-BC36-4AD4-93EA-A5DE9C36F417}"
                Name="ProductID" />

    </FieldRefs>
  </ContentType>
</Elements>

Step4: Right click on the project. Add >> New Item >> Choose "List Definition from content type".
Name: ProductCT


In this project we have create just one content type. Therefore,  in the drop down list we will see only one content type. In addition, we will create instance of this list. 


Step5: Lets examine list definition list schema. 
As we have chose "List Definition from the content type", we will get all the fields such as Product Description and Product ID automatically as illustrated below: 


However, we should these custom fields in the view as well. Therefore, we add FieldRefs in the BaseViewID="1" view. 


Step6: List Definitions and instances description. 

We need to give a meaningful names to the list definitions and instances. Also, in type we should give a number more than 10000.

Elements.xml (List Definition)

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <!-- Do not change the value of the Name attribute below. If it does not match the folder name of the List Definition project item, an error will occur when the project is run. -->
    <ListTemplate
        Name="ProductListDefinition"
        Type="10005"
        BaseType="0"
        OnQuickLaunch="TRUE"
        SecurityBits="11"
        Sequence="410"
        DisplayName="ProductListDefinition"
        Description="My List Definition"
        Image="/_layouts/images/itgen.png"/>
</Elements>

Elements.xml (List Instances)
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <ListInstance Title="Products"
                OnQuickLaunch="TRUE"
                TemplateType="10005"
                Url="Lists/Products"
                Description="My List Instance">
  </ListInstance>
</Elements> 

 The project structure will look like this:  

Build and Deploy the solution. 
Navigate to the site to view the new content type and list instance. 


Cheers, 
--aaroh 

Download the source here.

1 comment:

Anonymous said...

Just wanted to let you know that your rename of the Title column didn't work. In fact it won't work ever unless you set Inherits="FALSE".

How to display Sharegate administrative dynamic reports via Power BI on SharePoint Online

After working in the SharePoint domain for a couple of years as SharePoint consultant , I am fortunate enough to help customers from man...