Tuesday, May 15, 2007

SharePoint 2007 How To - Create a custom list definition

The SharePoint SDK shows How to - Create a Custom List Definition but is strange enough not complete. If you follow the SDK sample - the extra field you created in your custom list definition will NOT show up in the SharePoint form pages (Edit item, new item and view item).

In the next posting, I will describe in short the different steps - the last step is the one which I think is missing.

  • Create a new feature as explained in the SharePoint SDK. Afterwards install and activate the feature.
  • Modify the SCHEMA.XML file - Add your custom field inthe Fields section

<Fields>
<Field Type="Choice" Name="FavoriteColor" DisplayName="Favorite Color"

ID="{5F11FE7B-C8D4-4849-A400-133352D17ECF}">
<CHOICES>
<CHOICE>Red</CHOICE>
<CHOICE>Green</CHOICE>
<CHOICE>Blue</CHOICE>
</CHOICES>
</Field>
</Fields>


  • Add a reference to the Field you defined within the ViewFields section


<ViewFields>
  <FieldRef Name="Attachments"></FieldRef>
  <FieldRef Name="LinkTitle"></FieldRef>
  <FieldRef Name="FavoriteColor"></FieldRef>
</ViewFields>



  • Modify the ContentTypes section and modify the Item contenttype to include the custom column. This change will make sure that the custom column will also show up in the edit and new forms.

 


<ContentTypes>
<ContentTypeRef ID="0x0120" />
<ContentType ID="0x0100EE310F92BD4F4ee79829A2FB3A01F1ED" Name="Item"
Group="List Content Types" Description="Create a new list item." Version="2" 
FeatureId="{695b6570-a48b-4a8e-8ea5-26ea7fc1d162}">
<FieldRefs>
<FieldRef ID="{c042a256-787d-4a6f-8a8a-cf6ab767f12d}" Name="ContentType" />
<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="TRUE"
               ShowInNewForm="TRUE" ShowInEditForm="TRUE" />
<FieldRef ID="{5F11FE7B-C8D4-4849-A400-133352D17ECF}" Name="FavoriteColor"/>
</FieldRefs>
<XmlDocuments>
<XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
<Display>ListForm</Display>
<Edit>ListForm</Edit>
<New>ListForm</New>
</FormTemplates>
</XmlDocument>
</XmlDocuments>
<Folder TargetName="Item" />
</ContentType>
</ContentTypes>


  • Finally create a new list using the new custom SharePoint list definition

16 comments:

Anonymous said...

OH-MY-GOD, you just totally saved my life. stupid sdk.

Ali Khan said...

Field Type="Choice" Name="FavoriteColor" DisplayName="Favorite Color"

You can add "ShowInNewForm" and "ShowInEditForm" here to be true for both attributes and it will show up in the add/edit forms.

sparou said...

THANK YOU. I've been searching for this answer all over.

Kumarakuru said...

You could have posted the completed Schema.xml file and you are also partially posting information

jopx said...

The complete schema.xml would be a little bit too big ... ;-) What are you missing in the solution described?

Kumarakuru said...

See, I was trying to Just follow the Steps (like everyone) in the MSDN blog, and then at the schema.xml, as you said it just ends without saying anything. So If you could provide one completed Schema.xml (referring to survey list or custom list or too huge), every one would be benefitted. You can provide a link to the file.

As well, if we refer to your blog, you are giving so many tags, but we dont know what is the order, where it has to appear, what is the relation between them etc.,

Otherwise, Sharepoint ends saying "Cannot complete" and we dont know what is missing in the schema.xml, when we try to create new List

Anonymous said...

I tried your solution and the msdn solution the problem i get is that when i installed and activated the feature.

i want to create a list when i click on it to add it i receive a error.
If i look in the event log i see 2 things 1 access denied to the hosts file and 2 runtimefilter error do you have any idea what is causing this error?

thanx in advance

jopx said...

Kiteboarder ... are you sure the errors are related?

Can you post some more error information?

Tobias Abdon said...

When I run the command to install the feature I get an error "The given path's format is not supported."

Anyone know what this is about?

Anonymous said...

Wouldnt modifying the schema.xml file affect all sites in that server? Wouldnt it be unghosted? How do we make it work on a single site?

jopx said...

You should create a custom list definition, do not modify the schema.xml of one of the existing list templates.

There is no issue with unghosting since you are working directly on the list definition files. If you want to make a change to a single list, you can do it with SharePoint Designer but this will unghost your pages for this site. In 2007, this is not that big of an issue as it was in 2003

Sjoert Ebben Msc said...

Leave out the <ContentTypeRef ID="0x0120" /> from the ContentType section to make sure your columns are visible in edit mode.

Anonymous said...

Thanks thanks thanks :)

Anonymous said...

Thanks! A lot!

Unknown said...

Hi,

This works well, but when I go back and try to add another field to the schema, deactivate, uninstall, and re-install and re-activate the feature, my new custom field doesn't show up. It's like once the list template/content type is defined you can't change it. Have you tried this and run into any problems? The only work-around I've found is to change all the names & guids (I even have to change the feature name).

Thanks!

Unknown said...

According to the documentation for the element, the only child element is the element, however, in this code and in many other examples I have seen, I find the element is being used. Does anybody know what is the difference in behaviour.

Thanks,

Naamat