Udostępnij za pośrednictwem


SPContentType.Delete Method

Deletes the content type.

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

Syntax

'Declaration
<ClientCallableMethodAttribute(Name := "DeleteObject", RemoveThisObjectFromParentCollection := True)> _
<ClientCallableExceptionConstraintAttribute(FixedId := "3", ErrorCode := , Condition := "The content type is sealed and there is no list associated with it",  _
    ErrorType := GetType(SPContentTypeSealedException), ErrorValue := "The content type is sealed.")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "4", ErrorCode := , Condition := "The content type is readonly",  _
    ErrorType := GetType(SPContentTypeReadOnlyException), ErrorValue := "The content type is read only.")> _
<ClientCallableExceptionConstraintAttribute(FixedId := "1", ErrorCode := , Condition := "", ErrorType := GetType(SPException),  _
    ErrorValue := "Can not remove content type.")> _
Public Sub Delete
'Usage
Dim instance As SPContentType

instance.Delete()
[ClientCallableMethodAttribute(Name = "DeleteObject", RemoveThisObjectFromParentCollection = true)]
[ClientCallableExceptionConstraintAttribute(FixedId = "3", ErrorCode = , Condition = "The content type is sealed and there is no list associated with it", 
    ErrorType = typeof(SPContentTypeSealedException), ErrorValue = "The content type is sealed.")]
[ClientCallableExceptionConstraintAttribute(FixedId = "4", ErrorCode = , Condition = "The content type is readonly", 
    ErrorType = typeof(SPContentTypeReadOnlyException), ErrorValue = "The content type is read only.")]
[ClientCallableExceptionConstraintAttribute(FixedId = "1", ErrorCode = , Condition = "", ErrorType = typeof(SPException), 
    ErrorValue = "Can not remove content type.")]
public void Delete()

Exceptions

Exception Condition
SPException

The parent collection of the content type is read-only.

-or-

The content type is the last content type for the SPList on which the parent collection of the content type resides.

-or-

The content type is in use.

-or-

The content type is part of an application feature.

ArgumentOutOfRangeException

The content type has already been deleted from its parent collection.

SPContentTypeSealedException

The content type is sealed.

SPContentTypeReadOnlyException

The content type is read-only.

Remarks

You cannot delete a site content type if it is being used as the basis for other site or list content types. You must first remove this content type from all lists that use it and then delete all child site content types that are based on it.

You cannot delete a content type from a list if that list contains items of that content type. SharePoint Foundation does not consider items sent to the Recycle Bin when making this determination. If items of a specific content type are restored to a list after their content type has been deleted from that list, then those items are assigned the default content type for that list.

Tip

You might want to consider an alternative to deleting items simply because their content type is obsolete. Try leaving the outmoded content type in place and setting its Hidden property to true. This removes the content type from the New menu on any list where the content type is used, thus preventing users from adding new items of that content type to the list while at the same time preserving the content of existing items.

Examples

The following example consists of two methods from a larger application. The first method, DeleteListContentType, accepts an SPContentType object as its only argument. The method first verifies that the object is derived from a list’s content type collection. Then the method calls a second method, DeleteListItems, passing the list and the content type ID as arguments. The second method searches the list for items of the specified content type and deletes them. When control is returned to the DeleteListContentType method, it deletes the content type.

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

SPContentType Class

SPContentType Members

Microsoft.SharePoint Namespace

Delete

Hidden

Other Resources

Deleting Content Types