How to Delete Views and Folders

Updated: January 31, 2012

Applies To: System Center 2012 - Operations Manager

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 (EnterpriseManagementException error)
			{
				Console.WriteLine(error.Message);
		}
	}

		//---------------------------------------------------------------------
		private static void DeleteFolder(
			ManagementGroup mg,
			string folderDisplayName
			)
		{

			ManagementPackFolderCriteria folderCriteria = new ManagementPackFolderCriteria(
				string.Format("DisplayName='{0}'", folderDisplayName));
			IList<ManagementPackFolder> folders = mg.Presentation.GetFolders(folderCriteria);

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

			ManagementPackFolder 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
			)
		{
			IList<ManagementPackView> views;
			ManagementPackViewCriteria viewCriteria;
			ManagementPackView view;

			viewCriteria = new ManagementPackViewCriteria(
				string.Format("DisplayName='{0}'", viewDisplayName));
			views = mg.Presentation.GetViews(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();
	}
}
}

See Also