Share via


SPFieldLinkCollection.Delete Method (Guid)

Deletes the SPFieldLink object with the specified ID from the collection.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online

Syntax

'Declaration
Public Sub Delete ( _
    id As Guid _
)
'Usage
Dim instance As SPFieldLinkCollection
Dim id As Guid

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

Parameters

  • id
    Type: System.Guid

    The value of the Id property of the SPFieldLink object to delete.

Remarks

If an object with the specified ID is not found, the method takes no action.

Note

The object is not actually deleted until you call the SPContentType.Update method. As you make changes to a content type through the object model, your code actually makes those changes to the in-memory representation of the content type. Only after you call the Update method does SharePoint Foundation make the changes permanent, by writing them back to the content type definition that is stored in the database.

Examples

The following example shows a console application that enumerates all content types in the site collection, looking for references to a site field named “Owner.” If one is found, the application attempts to delete the SPFieldLink object from the site content type and all child content types. The application reports exceptions by printing messages to the console.

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

See Also

Reference

SPFieldLinkCollection Class

SPFieldLinkCollection Members

Delete Overload

Microsoft.SharePoint Namespace

Item[Guid]

SPFieldLink

SPContentType

Other Resources

Fields and Field References

Introduction to Columns