Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

SPFieldLink class

Represents a single column (also known as field) reference that is included in a content type.

System.Object
  Microsoft.SharePoint.SPFieldLink

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
[SubsetCallableTypeAttribute]
public class SPFieldLink

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 separately and then reference it in the content type definition. As a result, when you add a column to a content type, the content type contains a reference to the column (or field), rather than the column (or field) itself.

The reference contains a subset of the properties of the full field definition. These are the properties you can customize for the column as it exists in the content type.

For more information, see Fields and Field References.

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

The SPFieldCollection object provides you with a combined view of a column's attributes, as they exist in the specified 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 an SPField object in a content type, SharePoint Foundation combines the field definition with the field reference and returns the resulting SPField object. This is helpful because you do not have to look up a field definition and all the attributes in the field definition that are overridden by the field reference for that content type.

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

NoteNote

SharePoint Foundation does not support inheriting from the SPFieldLink class.

The following example shows a console application that creates a content type, applies it to a list, and adds references to fields at both the site and list level.

using System;
using Microsoft.SharePoint;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string contentTypeName = "Spec";
                    string listTitle = "Product Specs";
                    SPContentType docType = web.AvailableContentTypes["Document"];

                    // Create a content type.
                    SPContentType siteContentType = new SPContentType(docType,
                                                                      web.ContentTypes,
                                                                      contentTypeName);
                    // Add the content type to the site collection.
                    siteContentType = web.ContentTypes.Add(siteContentType);

                    // Change the name of the Title field in the content type.
                    siteContentType.FieldLinks["Title"].DisplayName = "Product";

                    // Add a field to the site field collection.
                    SPField siteField = AddField("Owner", SPFieldType.Text, web.Fields);

                    // Reference the field in the content type.
                    AddFieldLink(siteField, siteContentType);
                    siteContentType.Update();

                    // Create and configure a document library. 
                    Guid listId = web.Lists.Add(listTitle,
                                                "A library of product specifications",
                                                 SPListTemplateType.DocumentLibrary);
                    SPDocumentLibrary library = (SPDocumentLibrary)web.Lists[listId];
                    library.OnQuickLaunch = true;
                    library.ContentTypesEnabled = true;
                    library.EnableFolderCreation = false; // Hide "Folder" on the New menu
                    library.Update();

                    // Add the site content type to the library.
                    SPContentType listContentType = library.ContentTypes.Add(siteContentType);
                    library.Update();

                    // Add a field to the list field collection.
                    SPField listField = AddField("SKU", SPFieldType.Text, library.Fields);

                    // Reference the field in the list version of the content type.
                    AddFieldLink(listField, listContentType);
                    listContentType.Update();

                    // Remove the document content type from the library.
                    SPContentTypeId id = library.ContentTypes[docType.Name].Id;
                    library.ContentTypes.Delete(id);
                    library.Update();

                    // Review our work.
                    Console.WriteLine("Site content type");
                    PrintFieldNames(siteContentType);
                    Console.WriteLine("List content type");
                    PrintFieldNames(listContentType);
                }
            }
            Console.Write("Press ENTER to continue...");
            Console.ReadLine();
        }

        static SPField AddField(string title, SPFieldType type, SPFieldCollection fields)
        {
            // If the field is not in the collection,
            if (!fields.ContainsField(title))
            {
                // Add it.
                fields.Add(title, type, false);
            }
            return fields.GetField(title);
        }

        static void AddFieldLink(SPField field, SPContentType contentType)
        {
            // Is the FieldLink in the collection?
            SPFieldLink fieldLink = contentType.FieldLinks[field.Id];
            if (fieldLink == null) // No, so add it.
            {
                fieldLink = new SPFieldLink(field);
                contentType.FieldLinks.Add(fieldLink);
            }
        }

        static void PrintFieldNames(SPContentType contentType)
        {
            foreach (SPFieldLink link in contentType.FieldLinks)
            {
                SPField field = contentType.Fields[link.Id];
                // Screen out system fields.
                if (field.Reorderable && field.InternalName != "FileLeafRef")
                    Console.WriteLine("FieldLink.DisplayName = {0}", link.DisplayName);
            }
            Console.WriteLine();
        }
    }
}

The application prints the following output to the console.

Site content type
FieldLink.DisplayName = Product
FieldLink.DisplayName = Owner

List content type
FieldLink.DisplayName = Product
FieldLink.DisplayName = Owner
FieldLink.DisplayName = SKU

Press ENTER to continue...
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.