Let’s say you need to create a site definition which will satisfy the following requirements:
- Define List Definition/Instance (Part I)
- Define Site definition (Part II)
- Activate some features on site creation using Site Provision (Part III)
Your client wants to have a site definition that will add a site template in the ‘create web site’ list and when user will create a site based on the site definition, a list will be created and few features will be activated. In this post I’ll focus on creating a list definition and list instance.
Create list definition and List Instance
Step 1: Add List Definition and List Instance to the project: To create a list definition along with list instance add a List Definition item in SharePoint Project as shown below. For easy understanding, you can put Def at the end of the list definition name.
Figure 1: Create List Definition item
Later you’ll be prompted for list name, list definition type and ‘if a list instance will be created’ option as shown below. You may need to modify the list definition name, and list definition type. Also make sure the ‘Add a list instance for this list definition’ is selected as shown below.
Figure 2: List Definition Creation dialog
Step 2: Set List Instance Name and Title: After the list definition and list instance are added to the project, you will find the list instance name is generated automatically which you want to rename as shown below:
Figure 3: Rename auto-generated list instance name
Then focus on modifying the List Instance elements.xml file. At least modify the list title and URL as shown below:
Figure 4: Rename list instance title and url in elements.xml file of the list instance.
Now you have added the list definition and list instance and your next step is to define the list
Step 3: Define Template Type: Now you have added the list definition and list instance. First of all make sure you have put a type id for list definition and used that type id in list instance as shown below. I’ve set the template type in the left file (list definition) as (Type=10001) and the same type is used in the right file (list instance) as shown below:
Figure 5: Template Type id in List definition and list instance Elements.xml file (side by side)
Step 4: Add fields in the list definition: Now you are ready to add fields to the list definition. For this open the Schema.xml file under list definition folder and move the position between <Fields></Fields> as shown below:
Figure 6: Add Fields in list definition
So you are ready to add fields but it’ll be tough to add all fields just by typing manually. There’s trick I use often:
- Create a test list with the required fields
- Save the list as template
- Download the list template from template gallery.
- Rename the template file extension from .stp to .cab
- Open the cab file and extract the Manifest.xml file.
- Finally I take the field definitions from the Manifest.xml file
Step 5: Add fields to default view: In this step, you will add the fields to the default view (or any other view you want). If you take a look at the schema.xml file you will find more than one views are defined in the schema file. However, the view with DefaultView=”True” is the default view. Normally the view with BaseViewID 1 is the default view as shown below:
Figure 7: Default View with BaseViewID property value set to 1
So you have found the default view and now you need to add the field reference in the default view. As shown below, you can either user GUID or Field Name for referencing a field in ViewFields:
Figure 8: Add Field Ref in ViewField either by Field Name or ID
Though you can use either Field Name or ID for referencing a field in ViewFields, I prefer ID as the name can be changed/modified or XML encoded.
Step 6: Associate List definition and list instance with Two Features: Now you have two elements files: One is for list definition and another one is for list instance. With SharePoint architecture in mind, we need to activate the list definition in Site collection. Whereas the list instance needs to be created in individual web. So add two features and associate the elements.xml (FYI, elements.xml file can be associated with features) with features.
- We need to add a feature with site collection scope, and need to associate the list definition with that feature. In my case the feature ID is 574b2a16-b01d-4e30-97ed-d1c4a5aa37ea. You are not done yet with the list instance, you need to take the feature id (in which the list definition is attached) and put the value in List Instance elements.xml file as shown below:
Figure 9: List Instance Elements.xml file with list definition Feature ID
- We need to add another feature with web scope and need to associate the list instance with the feature
The following Figure shows the List Definition Feature:
Figure 10: List Definition Feature with Site Collection Scope (left circle) and ListDef item added (right circle)
The following figure shows the List Instance Feature:
Figure 11: List Instance Feature with Web scoped (left circle) and list instance item added (right circle)
So after this steps we get two final outputs (i.e., two features) that will be used in site definition. So collect the two feature IDs (you can get the feature ID by selecting and then opening the properties window). In my case the List Definition feature ID was 574b2a16-b01d-4e30-97ed-d1c4a5aa37ea and List Instance Feature ID was 161c4ebd-97b7-4dff-9734-c930434d3e95.
Until now you have developed one list definition, one list instance and two features: one for list definition and one for list instance. in the next post I’ll explain how to staple these list definition/instance with site definition.