SPContentType.ParentList property
取得表示這個SPContentType物件存在於的清單的SPList物件。
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public ReadOnly Property ParentList As SPList
Get
'用途
Dim instance As SPContentType
Dim value As SPList
value = instance.ParentList
public SPList ParentList { get; }
Property value
Type: Microsoft.SharePoint.SPList
這個內容型別存在於清單。
備註
這個屬性的值是a null reference (Nothing in Visual Basic)站台內容型別。清單的內容型別,值會是SPList物件,表示已新增的內容類型的清單或文件庫。如需詳細資訊,請參閱Site and List Content Types。
Examples
下列範例是由一種方法, DeleteListContentType所組成。如同其名稱會指出,方法的目的就是從清單中刪除內容的型別。它所接受的SPContentType物件做為唯一的引數。與其任務之前,這個方法會檢查以確認當做引數傳入的物件包含在清單的內容型別集合的ParentList屬性的值。如果屬性傳回的a null reference (Nothing in 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();
}
請參閱
參照
Microsoft.SharePoint namespace