How to: Add a Column to a Site
Applies to: SharePoint Foundation 2010
You can specify columns in a custom site definition configuration so that each time a user provisions a site of that type, Microsoft SharePoint Foundation includes the column as an available site column.
Tip
After a user has provisioned a site, you can add site columns to that site through the SharePoint Foundation object model.
Registering a Site Column in a Site Definition
To ensure that a site column is available on every site created from a specific custom site definition configuration, you must add some Collaborative Application Markup Language (CAML) markup to the site definition configuration in the Onet.xml file of the site definition. Then, when a user provisions a site of that type, the column is included by default in the site column collection.
Warning
Modifying the Onet.xml file for any of the built-in site types of SharePoint Foundation is not supported, so the following procedure can be used only on custom site types. For more information about creating custom site definition configurations, see How to: Create a Custom Site Definition and Configuration.
To specify a site column in a site definition configuration
Create the field as a separate Feature.
For more information, see Using Features in SharePoint Foundation and Field Definitions.
Reference that Feature in the Configuration element (in the Onet.xml file) that defines your custom site type:
Open the Onet.xml file in %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates\site_type\XML, where site_type is name of the custom site definition.
In the Configuration element add a reference to the site column Feature in either the SiteFeatures or WebFeatures element. You identify the Feature by its GUID. The following is an example.
<SiteFeatures> <Feature ID="00BFEA71-1C5E-4A24-B310-BA51C3EB7A57" /> <Feature ID="695B6570-ACDC-4A8E-8545-26EA7FC1D162" /> </SiteFeatures> <WebFeatures> <Feature ID="00BFEA71-4EA5-48D4-A4AD-7EA5C011ABE5" /> <Feature ID="00BFEA71-E717-4E80-DEAF-D0C71B360101" /> </WebFeatures>
For more information on the difference between these two elements, see SiteFeatures Element (Site) and WebFeatures Element (Site).
If you make changes to a column that is provisioned, SharePoint Foundation never writes those changes back to the XML column definition itself. Instead, SharePoint Foundation stores updates made to columns as entries within the SharePoint Foundation database. For more information, see Updating Site Columns.
Adding a Column to a Provisioned Site
You can use the SharePoint Foundation object model to programmatically add columns to provisioned sites.
To add a column to a provisioned site
To access the collection of site columns on the specified site, use the Fields property. This property returns an SPFieldCollection object.
To add a new column to the site, use one of the following methods of the returned SPFieldCollection object:
See Also
Tasks
How to: Add a Column to a List
How to: Reference a Column in a Content Type