Monday, April 4, 2011

All About List/Site Definition– Part I: Create List Definition/Instance with Visual Studio 2010

Let’s say you need to create a site definition which will satisfy the following requirements:

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.

image

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.

image

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:

image

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:

image

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:

image

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:

image

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:

  1. Create a test list with the required fields
  2. Save the list as template
  3. Download the list template from template gallery.
  4. Rename the template file extension from .stp to .cab
  5. Open the cab file and extract the Manifest.xml file.
  6. 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:

image

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:

image

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:
    image
    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:

image

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:

image

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.

Conclusion

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.

12 comments:

  1. Hi Sohel's
    I refer ur blog for creating Multiple List Definition using same ListDefinitionFeature and ListInstanceFeature. But now i am facing problem that when i am activating this feature the list is not the same view as i have in my old List and also these new fields are not present in my Newand Edit forms of the List, Can you please let me know what else i need to change in my List Definition Schema.xml

    ReplyDelete
  2. Divya,

    Is the feature working for single list definition and list instance?

    ReplyDelete
  3. yes Sohel Feature is working for Single List Definition and listInstance, but following the same problem with that. For example i created custom List with new column Height,Width then i save list as ListTemplate, then i create ListDefinition and ListInstance Feature. Install Feature in my new SiteCollection, Activate ListInstance Feature it created my customList but only with Title column, as i can see my field as a column in it,but not on Allitems view neither on New form,or edit or Display form, my question is do i need to copy tags other than ,
    Please suggest me because this is causing problem on query also,i cannot found the new fields in that also
    How to resolve?
    Thanks in Advance

    ReplyDelete
  4. Divya, There might be two cases: the fields are added to the list but not on the view. Another problem might be the fields are not added yet. Please check list settings page to find out if the fields are added or not in the list.

    ReplyDelete
  5. This is one of the best articles so far I have read online. No crap, just useful information. Very well presented. Thanks for sharing with us. Its really helpful beginner as well as developer. Check this helpful link too its containing a wonderful explanation on list definition in sharepoint 2010...

    http://mindstick.com/Articles/db52b9c2-9775-4899-a1f3-4f85fce9ec32/?List%20Definition%20in%20SharePoint%202010

    Thanks

    ReplyDelete
  6. nice topic thank you and thumb up !
    sbo

    ReplyDelete
  7. hey check this new website www.countcode.com. It's a social network made for programmers, where you can download,share or upload source codes, where you can count your own code lines for free. You have access to the web forum and the web chatroom. we are happy to have you joined to our community!

    ReplyDelete
  8. Hi Sohel,

    Thanks for the Post. Its is working fine.
    But the Custom List is getting created in Top Level site. How to restrict being created.
    Thanks
    Charan

    ReplyDelete
  9. @Charan, The list creation is bound to the feature it's associated with. You need to associate the list creation elements to a feature that is web scoped. As a result when the web-scoped feature will be activated any web, the list will be created. However, the list definition element should be incorporated with site-scoped.

    ReplyDelete
  10. Hi Sohel,
    OnQuickLaunch="TRUE" does not include the list in quick launch. Help pls

    ReplyDelete
  11. Thank you for ListDefinition ==> FeatureID :D

    ReplyDelete