SPFieldLink-Klasse

Stellt einen einzelne Spalte (auch bekannt als Feld) Verweis, der in einem Inhaltstyp enthalten ist.

Vererbungshierarchie

System.Object
  Microsoft.SharePoint.SPFieldLink

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

Syntax

'Declaration
Public Class SPFieldLink
'Usage
Dim instance As SPFieldLink
public class SPFieldLink

Hinweise

Wenn Sie bestimmte Elementmetadaten in einem Inhaltstyp nachverfolgen möchten, können Sie eine Spalte verweisen, die die Metadaten darstellt. Sie können nicht jedoch eine Spalte in einem Inhaltstyp erstellen. Sie müssen die Spalte separat erstellen und dann in der Inhaltstypdefinition darauf verweisen. Vorkommen, wenn Sie eine Spalte zu einem Inhaltstyp hinzufügen, den Inhaltstyp enthält einen Verweis auf die Spalte (oder Feld) statt der Spalten-(oder) selbst.

Die Referenz enthält eine Teilmenge der Eigenschaften der Definition der vollständigen dar. Dies sind die Eigenschaften, die Sie für die Spalte anpassen können, wie sie in den Inhaltstyp vorhanden sind.

Weitere Informationen finden Sie unter Fields and Field References.

Das SPContentType -Objekt enthält eine SPFieldLinkCollection und ein SPFieldCollection -Objekt.

Das SPFieldCollection -Objekt enthält eine kombinierte Ansicht von Attributen für eine Spalte, wie sie in den angegebenen Inhaltstyp vorhanden sind. Jedes SPField -Objekt stellt alle Attribute einer Spalten- oder Definition, kombiniert mit diesen Attributen, die im Feldverweis für diesen Inhaltstyp überschrieben wurden.

Wenn Sie ein SPField -Objekt in einem Inhaltstyp zugreifen, SharePoint Foundation kombiniert die Felddefinition mit Feldverweis und gibt das resultierende SPField -Objekt zurück. Dies ist hilfreich, da Sie keine nachschlagen eine Felddefinition und alle Attribute in der Felddefinition, die durch den Feldverweis für diesen Inhaltstyp außer Kraft gesetzt werden.

Es ist eine 1: 1 Korrelation zwischen den Elementen in den SPFieldLinkCollection und SPFieldCollection -Objekten. Für jede SPFieldLink -Objekt, das Sie zu einem Inhaltstyp hinzufügen, fügt SharePoint Foundation ein entsprechendes SPField -Objekt, das die kombinierte Ansicht von dieser Spalte im Inhaltstyp gemäß darstellt.

Hinweis

SharePoint Foundation unterstützt nicht von der SPFieldLink -Klasse erben.

Beispiele

Das folgende Beispiel zeigt eine Konsolenanwendung, die einen Inhaltstyp erstellt, die auf eine Liste angewendet und fügt Verweise auf Felder auf die Website und die Liste der Ebene.

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();
        }
    }
}

Die folgende Ausgabe wird in der Konsole angezeigt.

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

Threadsicherheit

Alle öffentlichen static (Shared in Visual Basic) Member dieses Typs sind threadsicher. Die Threadsicherheit von Instanzmembern ist nicht gewährleistet.

Siehe auch

Referenz

SPFieldLink-Member

Microsoft.SharePoint-Namespace

Weitere Ressourcen

Fields and Field References

FieldRef Element (ContentType)

Introduction to Columns

Introduction to Content Types