SPFieldLink 类

表示内容类型中包含的单个列 (也称为字段) 引用。

继承层次结构

System.Object
  Microsoft.SharePoint.SPFieldLink

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Class SPFieldLink
用法
Dim instance As SPFieldLink
public class SPFieldLink

备注

如果您想要跟踪的内容类型中某些项元数据,可以引用代表该元数据的列。但是,您无法在内容类型 ; 创建列必须分别创建列,然后内容类型定义中引用它。结果是,向内容类型添加列,内容类型包含对的引用列 (或字段),而不是从列 (或字段) 本身。

引用包含的属性的完整的字段定义的子集。这些是您可以自定义列的内容类型中存在的属性。

有关详细信息,请参阅Fields and Field References

SPContentType对象包含SPFieldLinkCollectionSPFieldCollection对象。

SPFieldCollection对象使您的合并视图的列的属性,因为它们存在于指定的内容类型。每个SPField对象代表一个列或字段的定义,与该内容类型字段引用中已重写这些属性组合的所有属性。

在访问内容类型的SPField对象时, SharePoint Foundation组合具有该字段引用的字段定义,并返回结果的SPField对象。这是有用,因为您不必查找字段定义和字段定义,通过该内容类型的字段引用的所有属性。

没有SPFieldLinkCollectionSPFieldCollection对象中的项之间的 1-1 关联。对于每个SPFieldLink对象添加到内容类型, SharePoint Foundation添加相应SPField对象代表该列中的内容类型定义的组合的视图。

备注

SharePoint Foundation不支持从SPFieldLink类继承。

示例

下面的示例演示的控制台应用程序创建内容类型,将其应用于列表,并将字段的引用添加的网站和列表级别。

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
    Sub Main()
        Dim site As SPSite = New SPSite("https://localhost")
        Try
            Dim web As SPWeb = site.OpenWeb()
            Try
                Dim contentTypeName As String = "Spec"
                Dim listTitle As String = "Product Specs"
                Dim docType As SPContentType = web.AvailableContentTypes("Document")

                ' Create a content type.
                Dim siteContentType As 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.
                Dim siteField As SPField = AddField("Owner", SPFieldType.Text, web.Fields)

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

                ' Create and configure a document library. 
                Dim listId As Guid = web.Lists.Add(listTitle, _
                                                   "A library of product specifications", _
                                                    SPListTemplateType.DocumentLibrary)
                Dim library As SPDocumentLibrary = CType(web.Lists(listId), SPDocumentLibrary)
                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.
                Dim listContentType As SPContentType = library.ContentTypes.Add(siteContentType)

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

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

                ' Remove the document content type. 
                Dim id As SPContentTypeId = 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)
            Finally
                web.Dispose()
            End Try
        Finally
            site.Dispose()
        End Try
        Console.Write("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    Function AddField(ByVal title As String, ByVal type As SPFieldType, _
                      ByRef fields As SPFieldCollection) As SPField
        ' If the field is not in the collection,
        If Not (fields.ContainsField(title)) Then
            ' Add it.
            fields.Add(title, type, False)
        End If
        Return fields.GetField(title)
    End Function

    Sub AddFieldLink(ByRef field As SPField, ByRef contentType As SPContentType)
        ' Is the FieldLink in the collection?
        Dim fieldLink As SPFieldLink = contentType.FieldLinks(field.Id)
        If fieldLink Is Nothing Then ' No, so add it.
            fieldLink = New SPFieldLink(field)
            contentType.FieldLinks.Add(fieldLink)
        End If
    End Sub

    Sub PrintFieldNames(ByVal contentType As SPContentType)
        For Each link As SPFieldLink In contentType.FieldLinks
            Dim field As SPField = contentType.Fields(link.Id)
            ' Screen out system fields.
            If field.Reorderable AndAlso field.InternalName <> "FileLeafRef" Then
                Console.WriteLine("FieldLink.DisplayName = {0}", link.DisplayName)
            End If
        Next link
        Console.WriteLine()
    End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://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();
        }
    }
}

应用程序将以下输出显示到控制台上。

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...

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPFieldLink 成员

Microsoft.SharePoint 命名空间

其他资源

Fields and Field References

FieldRef Element (ContentType)

Introduction to Columns

Introduction to Content Types