Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
    
Thx alot for the nice explanation about the content of Elements.xml and Schema.xml. It helped mi alot! :-) –  user11654 Oct 26 '12 at 19:35

1 Answer 1

up vote 9 down vote accepted

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.

share|improve this answer
    
Thank you for the answer. It somewhat makes sense now, but in the <MetaData> section of the List I'm adding a reference to the content type. Why can't I add references to the fields in the schema instead of the definitions themselves? On that line of thought, why don't I copy the entire content type in the Schema.xml but only add a reference to it? Sorry for the questions but I'm sort of a newbie to Sharepoint and I really want to understand how it works to be able to develop solutions for it. –  JohnDoDo Oct 25 '11 at 10:44
    
See the edited answer, hope it's more clear now. –  SPArchaeologist Oct 25 '11 at 11:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.