SPContentType.ParentList Property
Gets an SPList object that represents the list in which this SPContentType object resides.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: Yes
Available in SharePoint Online
Syntax
'Declaration
Public ReadOnly Property ParentList As SPList
Get
'Usage
Dim instance As SPContentType
Dim value As SPList
value = instance.ParentList
public SPList ParentList { get; }
Property Value
Type: Microsoft.SharePoint.SPList
The list in which this content type resides.
Remarks
The value of this property is a null reference (Nothing in Visual Basic) for a site content type. For a list content type, the value is an SPList object that represents a list or document library to which the content type has been added. For more information, see Site and List Content Types.
Examples
The following example consists of a method, DeleteListContentType. As its name indicates, the method’s purpose is to delete a content type from a list. It accepts an SPContentType object as its only argument. Before proceeding with its task, the method checks the value of the ParentList property to verify that the object that was passed in as an argument is included in the list’s content type collection. If the property returns a a null reference (Nothing in Visual Basic) value, the content type does not belong to the list, and the method returns. If the property returns an SPList object, the method uses the object to complete its task.
Function DeleteListContentType(ByRef ct As SPContentType) As Boolean
' Make sure we have a content type.
If ct Is Nothing Then
Throw New ArgumentException("Content type is null.")
End If
' Make sure we have a list content type.
If ct.ParentList Is Nothing Then
Return False
End If
' Delete list items of this content type.
DeleteListItems(ct.ParentList, ct.Id)
' Check for read-only and sealed.
If ct.ReadOnly Then
ct.ReadOnly = False
End If
If ct.Sealed Then
ct.Sealed = False
End If
' Delete it.
ct.Delete()
Return True
End Function
Sub DeleteListItems(ByRef list As SPList, ByVal id As SPContentTypeId)
Dim items As SPListItemCollection = list.Items
Dim count As Integer = items.Count 'Count will change
Dim i As Integer
For i = count -1 To 0 Step i - 1
Dim item As SPListItem = items(i)
If item.ContentType.Id = id Then
item.Delete()
End If
Next
list.Update()
End Sub
static bool DeleteListContentType(SPContentType ct)
{
// Make sure we have a content type.
if (ct == null)
throw new ArgumentException("Content type is null.");
// Make sure we have a list content type
if (ct.ParentList == null)
return false;
// Delete list items of this content type.
DeleteListItems(ct.ParentList, ct.Id);
// Check for read-only and sealed.
if (ct.ReadOnly)
ct.ReadOnly = false;
if (ct.Sealed)
ct.Sealed = false;
// Delete it.
ct.Delete();
return true;
}
static void DeleteListItems(SPList list, SPContentTypeId id)
{
SPListItemCollection items = list.Items;
int count = items.Count; //Count will change
for (int i = count -1; i >= 0; i--)
{
SPListItem item = items[i];
if (item.ContentType.Id == id)
item.Delete();
}
list.Update();
}
See Also
Reference
Microsoft.SharePoint Namespace