SPFieldLinkCollection.Delete-Methode (Guid)
Löscht das SPFieldLink -Objekt mit der angegebenen ID aus der Auflistung.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
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
)
Parameter
id
Typ: System.GuidDer Wert der Eigenschaft Id des zu löschenden Objekts SPFieldLink .
Hinweise
Wenn ein Objekt mit der angegebenen ID gefunden wird, führt die Methode keine Aktion aus.
Hinweis
Das Objekt wird nicht tatsächlich gelöscht, wenn Sie die SPContentType.Update -Methode aufrufen. Wie Sie Änderungen an einem Inhaltstyp über das Objektmodell vornehmen, wird der Code diese Änderungen tatsächlich in der in-Memory-Darstellung des Inhaltstyps. Nur nach dem Aufruf der Update -Methode SharePoint Foundation die Änderungen vornehmen dauerhafter, nach deren Erstellung auf die Inhaltstypdefinition, die in der Datenbank gespeichert ist.
Beispiele
Das folgende Beispiel zeigt eine Konsolenanwendung, die Listet alle Inhaltstypen in der Websitesammlung nach Verweisen auf ein Websitefeld mit dem Namen "Besitzer". Wenn eine gefunden wird, versucht die Anwendung, das SPFieldLink -Objekt aus dem Websiteinhaltstyp und alle untergeordneten Inhaltstypen zu löschen. Die Anwendung meldet Ausnahmen durch Drucken Nachrichten in der Konsole angezeigt.
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.
}
}
}
Siehe auch
Referenz
Microsoft.SharePoint-Namespace