How to Delete Views and Folders

System Center

Updated: May 22, 2009

Applies To: Operations Manager 2007 R2, Operations Manager 2007 SP1, System Center Operations Manager 2007

You can delete views and folders from a Management Pack that is not sealed. The following example demonstrates how to delete a view and a folder that were previously created.

Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports Microsoft.EnterpriseManagement
Imports Microsoft.EnterpriseManagement.Configuration
Imports Microsoft.EnterpriseManagement.Common
Imports Microsoft.EnterpriseManagement.Monitoring


Namespace SDKSamples
    Class Program
        Public Overloads Shared Function Main(ByVal args() As String) As Integer

            Dim mg As ManagementGroup
            mg = New ManagementGroup("localhost")

            Try

                DeleteView(mg, "SampleView1")
                DeleteFolder(mg, "SampleFolder")

            Catch err As MonitoringException

                Console.WriteLine(err.Message)
            End Try
        End Function

        '---------------------------------------------------------------------
        Private Shared Sub DeleteFolder( _
            ByVal mg As ManagementGroup, _
            ByVal folderDisplayName As String)

            Dim folders As ReadOnlyCollection(Of MonitoringFolder)
            Dim folderCriteria As MonitoringFolderCriteria
            Dim folder As MonitoringFolder

            folderCriteria = New MonitoringFolderCriteria( _
                String.Format("DisplayName='{0}'", folderDisplayName))
            folders = mg.GetMonitoringFolders(folderCriteria)

            If (folders.Count <> 1) Then
                Throw New ApplicationException("Failed to retrieve the specified folder")
            End If

            folder = folders(0)

            If (folder.GetManagementPack().Sealed) Then

                Throw New ApplicationException( _
                    "Its not possible to delete folders in a sealed management pack")
            End If

            ' Mark the folder for deletion.
            folder.Status = ManagementPackElementStatus.PendingDelete

            ' Commit the changes.
            folder.GetManagementPack().AcceptChanges()
        End Sub

        '---------------------------------------------------------------------
        Private Shared Sub DeleteView( _
            ByVal mg As ManagementGroup, _
            ByVal viewDisplayName As String)

            Dim views As ReadOnlyCollection(Of MonitoringView)
            Dim viewCriteria As MonitoringViewCriteria
            Dim view As MonitoringView

            viewCriteria = New MonitoringViewCriteria( _
                String.Format("DisplayName='{0}'", viewDisplayName))
            views = mg.GetMonitoringViews(viewCriteria)

            If (views.Count <> 1) Then

                Throw New ApplicationException("Failed to retrieve the specified view")
            End If

            view = views(0)

            If (view.GetManagementPack().Sealed) Then

                Throw New ApplicationException( _
                    "Its not possible to delete views in a sealed management pack")
            End If

            ' Mark the view for deletion.
            view.Status = ManagementPackElementStatus.PendingDelete

            ' Commit the changes.
            view.GetManagementPack().AcceptChanges()
        End Sub
    End Class
End Namespace

Show: