Export (0) Print
Expand All

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.

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Common;
using Microsoft.EnterpriseManagement.Configuration;

namespace SDKSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementGroup mg;
            mg = new ManagementGroup("localhost");

            try
            {
                DeleteView(mg, "SampleView1");
                DeleteFolder(mg, "SampleFolder");
            }
            catch (MonitoringException error)
            {
                Console.WriteLine(error.Message);
            }
        }

        //---------------------------------------------------------------------
        private static void DeleteFolder(
            ManagementGroup mg,
            string folderDisplayName
            )
        {
            ReadOnlyCollection<MonitoringFolder> folders;
            MonitoringFolderCriteria folderCriteria;
            MonitoringFolder folder;

            folderCriteria = new MonitoringFolderCriteria(
                string.Format("DisplayName='{0}'", folderDisplayName));
            folders = mg.GetMonitoringFolders(folderCriteria);

            if (folders.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the specified folder");
            }

            folder = folders[0];

            if (folder.GetManagementPack().Sealed)
            {
                throw new ApplicationException(
                    "Its not possible to delete folders in a sealed management pack");
            }

            // Mark the folder for deletion.
            folder.Status = ManagementPackElementStatus.PendingDelete;

            // Commit the changes.
            folder.GetManagementPack().AcceptChanges();
        }

        //---------------------------------------------------------------------
        private static void DeleteView(
            ManagementGroup mg,
            string viewDisplayName
            )
        {
            ReadOnlyCollection<MonitoringView> views;
            MonitoringViewCriteria viewCriteria;
            MonitoringView view;

            viewCriteria = new MonitoringViewCriteria(
                string.Format("DisplayName='{0}'", viewDisplayName));
            views = mg.GetMonitoringViews(viewCriteria);

            if (views.Count != 1)
            {
                throw new ApplicationException("Failed to retrieve the specified view");
            }

            view = views[0];

            if (view.GetManagementPack().Sealed)
            {
                throw new ApplicationException(
                    "Its not possible to delete views in a sealed management pack");
            }

            // Mark the view for deletion.
            view.Status = ManagementPackElementStatus.PendingDelete;

            // Commit the changes.
            view.GetManagementPack().AcceptChanges();
        }
    }
}

Show:
© 2014 Microsoft