共用方式為


SPFieldLinkCollection.Delete method (Guid)

從集合中刪除具有指定之 ID 的SPFieldLink物件。

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

Syntax

'宣告
Public Sub Delete ( _
    id As Guid _
)
'用途
Dim instance As SPFieldLinkCollection
Dim id As Guid

instance.Delete(id)
public void Delete(
    Guid id
)

參數

  • id
    Type: System.Guid

    要刪除的SPFieldLink物件的 [ Id ] 屬性的值。

備註

如果找不到具有指定之 ID 的物件,這個方法會採取任何動作。

注意事項注意事項

直到您呼叫SPContentType.Update方法,不會實際刪除物件。您所做的變更到透過物件模型的內容類型,您的程式碼實際上是那些變更於記憶體中表示的內容類型。只有在您呼叫Update方法後沒有SharePoint Foundation進行的變更永久的藉由將它們寫回到儲存在資料庫中的內容類型定義。

Examples

下列範例會示範一個主控台應用程式,會列舉所有的內容類型在網站集合中,尋找參考到站台欄位命名為 「 擁有者 」。如果找到的話,應用程式會嘗試刪除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 fldName As String = "Owner"
                Dim id As Guid = GetFieldId(fldName, web.Fields)
                ' Try to delete links to the field from site content types.
                Dim found As Boolean = False
                For Each ct As SPContentType In web.ContentTypes
                    Dim fldLnk As SPFieldLink = ct.FieldLinks(id)
                    If fldLnk IsNot Nothing Then
                        found = True
                        Console.WriteLine("Content type {0} links to field {1}.", ct.Name, fldName)
                        ct.FieldLinks.Delete(id)
                        Try
                            ct.Update(True, True) ' Throws an exception if this or child is readonly or sealed.
                            Console.WriteLine("Field deleted from content type and all children.")
                        Catch ex As SPException
                            Console.Write("Field was not deleted from all instances of this content type. ")
                            Console.WriteLine(ex.Message)
                        End Try
                    End If
                Next ct
                If Not found Then
                    Console.WriteLine("No site content type links to field {0}", fldName)
                End If
            Finally
                web.Dispose()
            End Try
            Finally
                site.Dispose()
            End Try
        Console.Write(vbCrLf + "Press ENTER to continue...")
        Console.ReadLine()
    End Sub

    Function GetFieldId(ByVal name As String, ByVal fields As SPFieldCollection) As Guid
        Dim id As Guid = Guid.Empty
        Dim fld As SPField = Nothing
        Try
            fld = fields.GetField(name)
        Catch ex As ArgumentException
            Console.WriteLine("Exception thrown by a call to {0} with argument '{1}'", ex.TargetSite, name)
        End Try
        If fld IsNot Nothing Then
            id = fld.Id
        End If
        Return id 'Might be Guid.Empty
    End Function
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 fldName = "Owner";
                    Guid id = GetFieldId(fldName, web.Fields);
                    // Try to delete links to the field from site content types.
                    bool found = false;
                    foreach (SPContentType ct in web.ContentTypes)
                    {
                        SPFieldLink fldLnk = ct.FieldLinks[id];
                        if (fldLnk != null)
                        {
                            found = true;
                            Console.WriteLine("Content type {0} links to field {1}.",
                                              ct.Name, fldName);
                            ct.FieldLinks.Delete(id);
                            try
                            {
                                ct.Update(true, true); // Throws an exception if this or child is readonly or sealed.
                                Console.WriteLine("Field deleted from content type and all children.");
                            }
                            catch (SPException ex)
                            {
                                Console.Write("Field was not deleted from all instances of this content type. "); 
                                Console.WriteLine(ex.Message);
                            }
                        }
                    }
                    if (!found)
                        Console.WriteLine("No site content type links to field {0}", fldName);
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }

        static Guid GetFieldId(string name, SPFieldCollection fields)
        {
            Guid id = Guid.Empty;
            SPField fld = null;
            try
            {
                fld = fields.GetField(name);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("Exception thrown by a call to {0} with argument '{1}'", ex.TargetSite, name);
            }
            if (null != fld)
                id = fld.Id;
            return id; //Might be Guid.Empty.
        }
    }
}

請參閱

參照

SPFieldLinkCollection class

SPFieldLinkCollection members

Delete overload

Microsoft.SharePoint namespace

Item[Guid]

SPFieldLink

SPContentType

其他資源

Fields and Field References

Introduction to Columns