Share via


SPFieldLink Constructor

Initializes a new instance of the SPFieldLink class with the specified field.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Sub New ( _
    field As SPField _
)
'Usage
Dim field As SPField

Dim instance As New SPFieldLink(field)
public SPFieldLink(
    SPField field
)

Parameters

Remarks

When an SPFieldLink object is created, some of its properties are initialized to the values of corresponding properties of the SPField object that is passed as an argument to the constructor. The properties and their values are listed in the following table.

Property

Value from

Hidden

SPField.Hidden

Id

SPField.Id

Name

SPField.InternalName

Required

SPField.Required

Examples

The following example shows part of a console application that adds an SPField object to the site’s SPFieldCollection, then uses the object to create an SPFieldLink object, and adds that object to a content type’s SPFieldLinkCollection.

Note that before you can use a new field to create an SPFieldLink object, the field must be a member of a field collection at either the site or list level.

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

' Get 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")

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

' Get 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 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 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

SPFieldLink Class

SPFieldLink Members

Microsoft.SharePoint Namespace

Other Resources

Fields and Field References

FieldRef Element (ContentType)

Introduction to Columns

Introduction to Content Types