Getting Management Pack Elements using the SDK
Published Feb 15 2019 06:54 AM 260 Views
First published on TECHNET on May 26, 2011

There a a few different ways to get management pack elements using the SDK.  For this example, I’ll work with object templates, but the same pattern holds true for essentially all other management pack elements like classes, relationship types, rules, tasks, etc.  I’ve just created a simple console application to demonstrate this.  First I added the three DLLs in C:\Program Files\Microsoft System Center\Service Manager 2010\SDK Binaries on the management server to my project as references.  Then I added some using statements.


using Microsoft.EnterpriseManagement;
using Microsoft.EnterpriseManagement.Configuration;

The first is to get the management pack and then call .Get…..(). For example:


Version version = new Version("7.0.6555.0");
ManagementPack mp = emg.ManagementPacks.GetManagementPack("ServiceManager.ChangeManagement.Configuration", null, version);
ManagementPackObjectTemplate templateByMP = mp.GetObjectTemplate("StandardChangeRequest");
Console.WriteLine("Template by MP and Name:" + templateByMP.DisplayName);

The next option is to get the element by GUID:


ManagementPackObjectTemplate templateByGUID = emg.Templates.GetObjectTemplate(new Guid("9B82C3ED-384C-1D63-3952-05CE09C1790A"));
Console.WriteLine("Template by GUID:" + templateByGUID.DisplayName);

The element GUID will always be the same because it is a generated hash value based on the MP ID (including PublicKeyToken and MP  Name) and the management pack element name.


The last option is to use the ManagementPack….Criteria class. There is one of these classes for essentially all management pack elements.  It allows you to do a SQL query using any of the attributes of that particular type of management pack element.  All MP elements have a Name attribute so we can use that here:


ManagementPackObjectTemplateCriteria criteria = new ManagementPackObjectTemplateCriteria("Name = 'StandardChangeRequest'");
IList<ManagementPackObjectTemplate> templates = emg.Templates.GetObjectTemplates(criteria);
foreach (ManagementPackObjectTemplate templateByCriteria in templates)
{
Console.WriteLine("Template by Criteria:" + templateByCriteria.DisplayName);
}

This approach will get a IList of management pack elements that satisfy the criteria.  If you are searching by name you should only get one but if you wanted to you could use other criteria to get a bunch of templates.


ManagementPackObjectTemplateCriteria criteria = new ManagementPackObjectTemplateCriteria("Name like 'Standard%'");
IList<ManagementPackObjectTemplate> templates = emg.Templates.GetObjectTemplates(criteria);
foreach (ManagementPackObjectTemplate templateByCriteria in templates)
{
Console.WriteLine("Template by Criteria:" + templateByCriteria.DisplayName);
}

The Visual Studio project is attached.


GetTemplates.zip

Version history
Last update:
‎Mar 11 2019 08:47 AM
Updated by: