Question

I'm creating a custom list definition in SharePoint 2010 by following this article. The tutorial is OK and it works.

There is a thing that I don't understand though. Why do I need to insert the fields twice, once in the Elements.xml and once in the Schema.xml?

enter image description here

enter image description here

Can someone explain this?

Was it helpful?

Solution

Well, basically because in your CAML declarations you are performing three different operations. Let me explain this in a simpler way.

You first define the field as Site Columns in your element file (sample shows the declaration of one of the OOB field):

<Field ID="{8c06beca-0777-48f7-91c7-6da68bc07b69}"
    Name="Created"
    SourceID="http://schemas.microsoft.com/sharepoint/v3"
    StaticName="Created"
    Group="_Hidden"
    ColName="tp_Created"
    RowOrdinal="0"
    ReadOnly="TRUE"
    Type="DateTime"
    DisplayName="$Resources:core,Created;"
    StorageTZ="TRUE">

This results in your custom field begin added to the site collection field gallery - your field is available for use in the site collection but as now no-one is using it.

Next, you reference your field in a content type declaration:

<FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" Name="Title" Required="FALSE" ShowInNewForm="FALSE" ShowInEditForm="TRUE"/> 

In this case you aren't adding a new field but simply referencing an existing one to be used by your content type - aka your custom list definition.

Next let's move to the Schema.xml file. Here, as you noticed, you must re-add your file declaration - that because in this case you are adding your field to the list definition. SharePoint lists use copies of the site collection fields, so it not sufficient for the field to be available at the site collection level - you must add them explicitely to the schema definition so that the list can use them. You also need to reference the content type (which also gets copied) and that why you should use the ContentTypeRef tag. The reason for the difference in the two tag (if any - all can resolve to a simple incoerence in the naming convention used by the list definiton schema) is probably that while SharePoint allows user to add a field directly at a list level, content types are commonly created only at the site collection level (in that sense a list CT exists only as a child of the site collection CT). It may also be a simple disambiguation - a FieldRef tag is used in list definition as a child of the ViewFields element (to specify the list of field that a list view should include).

Hope that now the logic behind this will seem a litle clearer. Fell free to leave a comment if you need further explanation.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top