|
In previous posts I showed you how you can create site columns and content types by using SharePoint features. In this post I will describe how you can use this new contenttype in a custom list definition. We will create a new site column called "Region" and add that to the existing out of the box "Task" content type. After that we will create a new list template for our custom Task definition and add that to a new site definition. In our new sites, we will end up having a taks list with a "Region" field.
I will not describe how you create, deploy and active features, please see my previous posts for that.
These samples were built using beta2-TR. I tried re-using my beta2 samples, but I found some resource strings that were change going to TR, so I ended up recreating everything.
Step 1 - Create a site column for the Region field.
Create a new folder in the SharePoint FEATURES folder called "MyFields". Add the XML that defines the site column to a new xml file called MyFields.xml. Add a feature.xml file and reference Myfields.xml from the feature.xml. Activate the feature using STSADM.
The XML for the site column that we are using:
<? xml version="1.0" encoding="utf-8"?>< Elements xmlns="http://schemas.microsoft.com/sharepoint/">< Field ID="{9efcfb6d-138c-4e53-a5b1-8c05a59f0256}" Name="Region" Group="My Metadata Model" Type="Choice" DisplayName="Region" SourceID="http://schemas.microsoft.com/sharepoint/v3/fields" StaticName="Region" Description="" Sealed="TRUE" Required="TRUE" AllowDeletion="FALSE" FillInChoice="FALSE">< CHOICES>< CHOICE>ASPAC</CHOICE>< CHOICE>Americas</CHOICE>< CHOICE>EMA</CHOICE></ CHOICES>< Default>EMA</Default></ Field></ Elements>
Please see this post for detailed instructions.
Step 2 - Create a content type for our custom Task
In the SharePoint FEATURES folder, create a new subfolder called "MyContentTypes". Add a feature.xml file and create a file for the ElementManifest called "MyContentTypes.xml". Add the xml below to this file to create the content type. Activate the feature using STSADM. <?xml version="1.0" encoding="utf-8"?>< Elements xmlns="http://schemas.microsoft.com/sharepoint/">< ContentType ID="0x010810" Name="My Task" Group="My Content Types" Description="My Content Types for testing purposes" Version="0" V2ListTemplateName="tasks">< FieldRefs>< FieldRef ID="{9efcfb6d-138c-4e53-a5b1-8c05a59f0256}" Name="Region" Required="TRUE" /></ FieldRefs></ ContentType></ Elements>
The ID of our new content type is "0x010810". This means that our content type inherits from contenttype "0x0108", which is the out of the box "Task". In the FieldRefs element you will find a reference to the Region site column we just created. Make sure the ID's (guid) are equal.
This post has all the detailed instructions.
Step 3 - Create a feature that creates and registers the list template
In this step we create a new schema file for our custom list and we will register a new ListTemplate for our custom list. In SharePoint 2003 you used to do this in the onet.xml file for each site definition that needed the custom list. The SharePoint 2007 solution is much cleaner. You just create the list template and it's registration in a feature and activate this feature in the site definitions that need it. The result is that we have central management of our list template, instead of in a number LISTS folders across the file system.
First create a new folder in the FEATURES folder called "MyTasksList". The easiest way to do this, is copy the out of the box "TasksList" folder. Edit the "Tasks.xml" file in the "ListTemplates" folder. This xml will look familiar for those of you who created custom list templates in SharePoint V2. My xml looks like this: <? xml version="1.0" encoding="utf-8"?>< Elements xmlns="http://schemas.microsoft.com/sharepoint/">< ListTemplate Name="tasks" Type="5500" BaseType="0" OnQuickLaunch="TRUE" SecurityBits="11" Sequence="360" DisplayName="My Task" Description="My Task for testing purposes" Image="/_layouts/images/ittask.gif" /></ Elements>
The most important change is the new "Type" value. The value is important, because you will need it later.
The subfolder "Tasks" contains the schema file for our custom list. Edit this file and search for the "<ContentTypes>" element. You will find a ContentTypeRef element for the default Task content type. Change this to our custom content type:
< ContentTypes>< ContentTypeRef ID="0x010810">< Folder TargetName="Task" /></ ContentTypeRef>< ContentTypeRef ID="0x0120" /></ ContentTypes>
The next thing that you will have to do is not very logical in my opinion. You probably end up thinking 'why am I doing this????', but it is the only way to get it working. What you will need to do is register a new <Field> element in the <Fields> element, that is an exact copy of the <Field> element in the site definition of Step 1. This seemed a bit strange to me the first time I needed to do this. After all, we registered our custom content type, that knows which fields we have. I guess that we need this because the views are not part of a content type, and therefore the list schema needs to have the field definitions that can be registered in a view directly from the schema. If you don't do this step, you end up with a custom list that has the custom content type attached to the list, but does not have your custom fields. If you manually attach the content type to the list, it will work correctly. To get around it you have to add your fields to the schema: < Field ID="{9efcfb6d-138c-4e53-a5b1-8c05a59f0256}" Name="Region" Type="Choice" DisplayName="Region" SourceID="http://schemas.microsoft.com/sharepoint/v3/fields" StaticName="Region" Description="" Sealed="TRUE" Required="FALSE" AllowDeletion="FALSE" FillInChoice="FALSE">< CHOICES>< CHOICE>ASPAC</CHOICE>< CHOICE>Americas</CHOICE>< CHOICE>EMA</CHOICE></ CHOICES>< Default>EMA</Default></ Field>
If you keep the ID the same as the site column definition, your column will be based on the site column, so you will have all the benefits of using site columns. The WSS SDK contains a special note on this issue. The current version can be found here. Search for "How to: Add a Content Type to a List" and read the note carefully.
Step 4 - Create a new Site definition and add our custom list
This excellent blogpost by Todd Baginski explains how to create and register your custom site definition. Mine is called "MyTestSite". You need to change the onet.xml of your site definition. In the SiteFeatures you will need to activate the site column and content type features, created in step 1 and 2
< SiteFeatures><!-- MY TEST FEATURES -->< Feature ID="f95f506b-13cf-41ff-8ca9-7b2a19a34e83" /><!-- Site Column -->< Feature ID="95db9cfb-dd0b-4b18-8933-ff623a09adea" /><!-- Content Type --></ SiteFeatures>
Make sure that you activate the sitecolumn before the contenttype!
In the <Configuration> element of your onet.xml file you will also find the <Lists> element. In this element you can instantiate your list template to a list.
< Lists>< List Title="My Test Tasks" Description="My Test Tasks" Url="Lists/Tasks" Type="5500" FeatureId="406dc0e2-cad1-4063-bc5b-ce3e598bd24a" /></ Lists>
In the "Type" attribute, you need to put the same value as in the type attribute of the list template in step 3. The "FeatureID" attribute is equal to the ID of the feature "MyTasksList". After registering the site definition and an IISRESET, you are ready to test your new site definition!
Step 5 - Test
Create a new site. In you did not specify a tab, your template will show up in the "Custom" tab:
If you go to "View All Site Content" you will see a new list called "My Test Tasks":
Now click the new button, and you will see the new dropdown for the Region field:
I have attached a zip file with all the files that I used to create this blog. Enjoy! |