共用方式為


SPFieldCollection.Delete method

從集合中刪除欄位,具有指定內部、 顯示或靜態名稱。

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Sub Delete ( _
    strName As String _
)
'用途
Dim instance As SPFieldCollection
Dim strName As String

instance.Delete(strName)
public void Delete(
    string strName
)

參數

  • strName
    Type: System.String

    字串,指定內部、 顯示,或 staticname 或欄位的標題。SharePoint Foundation會先檢查指定的值,以內部的名稱,再依標題。

Exceptions

Exception Condition
SPException

此欄位是唯讀。

ArgumentException

該名稱沒有欄位存在集合中。

InvalidOperationException

欄位的CanBeDeleted屬性所傳回的值是false。

備註

這個方法會取得欄位的名稱從欄位的屬性,以下列順序: InternalNameTitleStaticName

如果欄位是類型SPFieldLookup ,且它是主要的查閱欄位,會一併刪除所有的次要查閱。如需詳細資訊,請參閱IsDependentLookup屬性。

Examples

下列程式碼範例會刪除具有指定名稱的欄位從 [事件] 清單中指定的網站下的所有子網站。

Dim siteCollection As SPSite = SPContext.Current.Site
Try
    Dim webSites As SPWebCollection = siteCollection.AllWebs("MySite").Webs

    Dim webSite As SPWeb
    For Each webSite In  webSites
        Dim fields As SPFieldCollection = 
            webSite.Lists("Announcements").Fields

        fields.Delete(fields("MyField").InternalName)
    Next webSite
Finally
    siteCollection.Dispose()
End Try
SPSite oSiteCollection = SPContext.Current.Site;
SPWebCollection collWebsites = oSiteCollection.AllWebs["MySite"].Webs;
foreach (SPWeb oWebsite in collWebsites)
{
    SPFieldCollection collFields = oWebsite.Lists["Announcements"].Fields;
    collFields.Delete(collFields["MyField"].InternalName);
}
注意事項注意事項

某些物件實作IDisposable介面,並且您必須避免之後不再需要保留這些物件在記憶體中。良好的程式碼撰寫方式的相關資訊,請參閱Disposing Objects

請參閱

參照

SPFieldCollection class

SPFieldCollection members

Microsoft.SharePoint namespace