SPContentType.ParentList 属性

获取一个SPList对象,该对象表示此SPContentType对象所在的列表。

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public ReadOnly Property ParentList As SPList
    Get
用法
Dim instance As SPContentType
Dim value As SPList

value = instance.ParentList
public SPList ParentList { get; }

属性值

类型:Microsoft.SharePoint.SPList
此内容类型所在的列表。

备注

The value of this property is 空引用(无 在 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.

示例

下面的示例包括一种方法, DeleteListContentType。正如其名称,该方法的用途是从列表中删除的内容类型。它作为其唯一参数接受SPContentType对象。其任务之前,该方法会检查确认作为参数传入的对象包含在列表的内容类型集合的ParentList属性的值。如果该属性返回一个空引用(无 在 Visual Basic 中)值,该内容类型不属于列表中,并且该方法返回。如果该属性返回SPList对象,该方法将使用对象完成其任务。

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();
}

另请参阅

引用

SPContentType 类

SPContentType 成员

Microsoft.SharePoint 命名空间

其他资源

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy