共用方式為


SPFieldLink class

代表包含在內容類型的 (也稱為] 欄位) 的單一欄參照。

Inheritance hierarchy

System.Object
  Microsoft.SharePoint.SPFieldLink

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

Syntax

'宣告
Public Class SPFieldLink
'用途
Dim instance As SPFieldLink
public class SPFieldLink

備註

如果您想要追蹤特定項目中的中繼資料的內容類型,您可以參考代表該中繼資料欄。不過,您無法建立一欄中的內容類型 ;您必須分開建立欄,然後再將它參照中的內容類型定義。因此,新增欄至內容類型之後,內容類型包含的參考資料行 (或欄位),而不是資料行 (或欄位) 本身擷取。

參照包含完整的欄位定義之屬性的子集。這些是您可以在存在於中的內容類型] 欄的自訂屬性。

For more information, see Fields and Field References.

SPContentType物件包含SPFieldLinkCollectionSPFieldCollection物件。

SPFieldCollection物件提供的一欄屬性的複合檢視存在於指定的內容類型中。每個SPField物件代表欄或欄位的定義,與該內容類型的欄位參考中有已覆寫這些屬性結合的所有的屬性。

當您在存取SPField物件中的內容類型時, SharePoint Foundation結合使用的欄位參照的欄位定義,並傳回所產生的SPField物件。這是有用,因為您沒有查閱欄位定義與所有屬性的欄位定義中,會覆寫該內容類型的欄位參照。

沒有 1-1 之間的相互關聯SPFieldLinkCollectionSPFieldCollection物件中的項目。針對您新增至內容類型每個SPFieldLink物件, SharePoint Foundation新增對應的SPField物件,代表該資料行中的內容類型所定義的複合的檢視。

注意事項注意事項

SharePoint Foundation不支援繼承自SPFieldLink類別。

Examples

下列範例會顯示主控台應用程式,會建立內容類型、 套用至清單,並將欄位的參照的網站和清單層級。

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

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPFieldLink members

Microsoft.SharePoint namespace

其他資源

Fields and Field References

FieldRef Element (ContentType)

Introduction to Columns

Introduction to Content Types