Follow us on Twitter
Follow us in Facebook
Office Dev Content
SharePoint Dev Content
Blogs for Office developers > SharePoint dev blog
The question in the title comes up so often, I suspect that everyone who creates a new list type for the first time encounters this problem:
Everything seems to be working, but …
When you click Add new item to create the first list item, you discover that your fields are not on the New form. If you create the item anyway and then try to edit it or display it, you find that your fields are not on the Edit or Display forms either.
The key to understanding the problem is to remember that list views and content types each have their own schema. When you open a list, SharePoint reads the schema of the current view from the view schema in the list’s schema.xml file to determine what fields should be displayed. But when you open the New, Edit, or Display forms for the list, SharePoint reads the schema of the item’s content type to determine what fields to display on the form. The implication of this is that when you want your fields to appear on the forms (as you almost always do):
You must add the fields to one or more content types as well as to views.
To do this declaratively, you begin by adding a Content Type item to your Visual Studio project. An elements.xml file is created with a <ContentType … > element. The attributes of the element have already been given values. Change them as needed, but leave the ID value that Visual Studio automatically generated as it is.
Visual Studio has also created a child <FieldRefs> section. For each of your new fields add a <FieldRef … > element to this section just as you did in the <ViewFields> sections of the schema.xml file.
Next, in the <ContentTypes> section of the schema.xml file, add a <ContentTypeRef … > element and assign your new content type’s ID to the ID attribute. Optionally, you can remove existing <ContentTypeRef … > elements if any of them represent content types that you do not want on the list.
Retract and redeploy your project. When you create, edit, or display an item of your new content type, your new fields will appear on the forms.
Note that you have to specify <FieldRef … > elements in the content type for only your new fields, not for the fields that it inherits from its base content type.
If your list and fields are being created programmatically, the same principle applies: new fields must be added to content types as well as the list if they are to be visible on the New, Edit, or Display forms. The Microsoft.SharePoint.SPFieldCollection.AddFieldAsXml method can be very helpful in this regard. You use it to add a field to a list and it has an overload that enables you to specify whether to also add the field to the list’s default content type, or to all the list’s content types (or to none of them).