Export (0) Print
Expand All

SPContentType.FieldLinks property

Gets an SPFieldLinkCollection that represents the collection of column, or field, references in the content type.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPFieldLinkCollection FieldLinks { get; }

Property value

Type: Microsoft.SharePoint.SPFieldLinkCollection
The collection of field references in the content type.

If you want to track certain item metadata in a content type, you can reference a column that represents that metadata. However, you cannot create a column in a content type; you must create the column and then reference it in the content type definition. As a result, when you add a column to a content type, the content type does not contain a column, or field; it contains a column, or field, reference.

For more information, see Fields and Field References.

The SPContentType object contains both a SPFieldLinkCollection and an SPFieldCollection object.

The SPFieldCollection object provides developers a way to get a combined view of a column's attributes, as they exist in that content type. Each SPField object represents all the attributes of a column, or field, definition, combined with those attributes that have been overridden in the field reference for that content type.

When you access a SPField in a content type, SharePoint Foundation merges the field definition with the field reference, and returns the resulting SPField object to you. This prevents developers from having to look up a field definition, and then look up all the attributes in the field definition overridden by the field reference for that content type.

Because of this, there is a 1-to-1 correlation between the items in the SPFieldLinkCollection and SPFieldCollection objects. For each SPFieldLink you add to a content type, SharePoint Foundation adds a corresponding SPField object that represents the merged view of that column as it's defined in the content type.

You cannot directly add or delete items from an SPFieldCollection object in an SPContentType object; if you try to do this, an error is thrown.

The following example is part of a console application that creates a new field and adds it to the site’s SPFieldCollection. Then the application creates a new SPFieldLink with a reference to the new column and adds it to a content type’s [SPFieldLinkCollection] collection. Finally, the application updates the content type, committing the changes to the database.

Note that a field must exist in the site’s field collection before it can be used to create an SPFieldLink object.

SPSite site = new SPSite("http://localhost");
SPWeb web = site.OpenWeb();

// Get a reference to the site content type collection.
SPContentTypeCollection contentTypes = web.ContentTypes;

// Create a Customer content type derived from the Contact content type.
SPContentType contentType = new SPContentType(contentTypes["Contact"], contentTypes, "Customer");

// Add the content type to the site collection.
contentTypes.Add(contentType);

// Get a reference to the site fields (columns) collection.
SPFieldCollection siteFields = web.Fields;

// Create a new field (column) and add it to the site collection.
string fieldName = siteFields.Add("Last Order", SPFieldType.DateTime, false);

// Create a reference to the new field.
SPFieldLink fieldLink = new SPFieldLink(siteFields.GetField(fieldName));

// Add the field reference to the content type.
contentType.FieldLinks.Add(fieldLink);

// Commit changes to the content type.
contentType.Update();

// Clean up
web.Dispose();
site.Dispose();
Show:
© 2014 Microsoft