ParentList property

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)

public SPList ParentList { get; }

Property value

Type: Microsoft.SharePoint.SPList
The list in which this content type resides.

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.

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.

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();
}
Show:
© 2016 Microsoft