共用方式為


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)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
<ClientCallableAttribute> _
Public ReadOnly Property FieldLinks As SPFieldLinkCollection
    Get
'Usage
Dim instance As SPContentType
Dim value As SPFieldLinkCollection

value = instance.FieldLinks
[ClientCallableAttribute]
public SPFieldLinkCollection FieldLinks { get; }

Property Value

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

Remarks

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.

Examples

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.

Dim site As SPSite = New SPSite("https://localhost")
Dim web As SPWeb = site.OpenWeb()

' Get a reference to the site content type collection.
Dim contentTypes As SPContentTypeCollection = web.ContentTypes

' Create a Customer content type derived from the Contact content type.
Dim contentType As SPContentType = New SPContentType(contentTypes("Contact"), contentTypes, "Customer")

' Put the content type in a group.
contentType.Group = "Test"

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

' Get a reference to the site fields (columns) collection
Dim siteFields As SPFieldCollection = web.Fields

' Create a new field (column) and add it to the site collection.
Dim fieldName As String = siteFields.Add("Last Order", SPFieldType.DateTime, False)

' Create a reference to the new field.
Dim fieldLink As SPFieldLink = 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()
SPSite site = new SPSite("https://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();

See Also

Reference

SPContentType Class

SPContentType Members

Microsoft.SharePoint Namespace

Other Resources

Fields and Field References

Introduction to Columns

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy