How to Add Resources to a Management Pack

System Center

Resources in a management pack are named references of files. While the file contains the content of the resource, the management pack only references the resources through the named identifier. The files are added to a management pack bundle, and when a management pack is installed through that bundle, the bundle will provide all the resource data needed. Alternatively a management pack XML can be imported through code together with a set of file streams match the resources in the management pack.

Resource Streams

Resources are always defined in the management pack and then added to System Center in another way. Each resource defined in the management pack will have an identifier which is used by the current management pack or by other management packs. The identifier should be unique for the management pack.

To add resource references to a management pack

  1. Load the management pack to gain reference to the management pack instance.

  2. Create three new instances of the ManagementPackImage class each with unique identifiers.

  3. Set the FileName property for each ManagementPackImageto the appropriate file name.

  4. Call the AcceptChanges method on the management pack.

The following example demonstrates adding image resource references to a management pack:

EnterpriseManagementGroup mg = new EnterpriseManagementGroup("localhost");
ManagementPack mp = new ManagementPack("Folder\\RePackaging.Library.xml", mg);

ManagementPackImage res1 = new ManagementPackImage(mp, "RePackaging.Library.Resources.Images.Package_16", ManagementPackAccessibility.Public);
ManagementPackImage res2 = new ManagementPackImage(mp, "RePackaging.Library.Resources.Images.Package_32", ManagementPackAccessibility.Public);
ManagementPackImage res3 = new ManagementPackImage(mp, "RePackaging.Library.Resources.Images.Package_64", ManagementPackAccessibility.Public);

res1.FileName = "package_16.png";
res2.FileName = "package_32.png";
res3.FileName = "package_64.png";

mp.AcceptChanges();

<ManagementPack ContentReadable="true" SchemaVersion="1.1" OriginalSchemaVersion="1.1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <Manifest>
    <Identity>
      <ID>RePackaging.Library</ID>
      <Version>1.0.0.0</Version>
    </Identity>
    <Name>RePackaging Library</Name>
    <References>
      <Reference Alias="WorkItem">
        <ID>System.WorkItem.Library</ID>
        <Version>7.0.6555.0</Version>
        <PublicKeyToken>9396306c2be7fcc4</PublicKeyToken>
      </Reference>
    </References>
  </Manifest>
  <TypeDefinitions>
    <EntityTypes>
      <ClassTypes>
        <ClassType ID="RePackaging.Request" Accessibility="Public" Abstract="false" Base="WorkItem!System.WorkItem" Hosted="false" Singleton="false" Extension="false">
          <Property ID="Requester" Type="string" AutoIncrement="false" Key="false" CaseSensitive="false" MaxLength="256" MinLength="0" Required="false" />
          <Property ID="SoftwareTitle" Type="string" AutoIncrement="false" Key="false" CaseSensitive="false" MaxLength="256" MinLength="0" Required="false" />
          <Property ID="BitsURI" Type="string" AutoIncrement="false" Key="false" CaseSensitive="false" MaxLength="256" MinLength="0" Required="false" />
        </ClassType>
      </ClassTypes>
    </EntityTypes>
  </TypeDefinitions>
  <LanguagePacks>
    <LanguagePack ID="ENU" IsDefault="false">
      <DisplayStrings>
        <DisplayString ElementID="RePackaging.Request" SubElementID="Requester">
          <Name>Requester</Name>
          <Description>The name of the person requesting the software.</Description>
        </DisplayString>
        <DisplayString ElementID="RePackaging.Request" SubElementID="SoftwareTitle">
          <Name>Software Title</Name>
          <Description>The title of the software.</Description>
        </DisplayString>
        <DisplayString ElementID="RePackaging.Request" SubElementID="BitsURI">
          <Name>Software Location</Name>
          <Description>The URI of the software.</Description>
        </DisplayString>
      </DisplayStrings>
    </LanguagePack>
  </LanguagePacks>
  <Resources>
    <Image ID="RePackaging.Library.Resources.Images.Package_16" Accessibility="Public" FileName="package_16.png" HasNullStream="false" />
    <Image ID="RePackaging.Library.Resources.Images.Package_32" Accessibility="Public" FileName="package_32.png" HasNullStream="false" />
    <Image ID="RePackaging.Library.Resources.Images.Package_64" Accessibility="Public" FileName="package_64.png" HasNullStream="false" />
  </Resources>
</ManagementPack>

Namespaces

Microsoft.EnterpriseManagement

Microsoft.EnterpriseManagement.Configuration

Assemblies

Microsoft.EnterpriseManagement.Core

Resource Streams in the Bundle

The actual resource data referenced by a management pack is added to System Center. This is completed in two forms. You can load the management pack xml, load each resource stream, and import the management pack with the resources. Or the resource streams can be added to a management pack bundle before the time of import. When the bundle is imported, the stream data will be imported together with all the management packs in the bundle.

To import a management pack with resources

  1. Load the management pack to gain reference to the management pack instance.

  2. Create three new FileStream object instances, one for each image file.

  3. Create three new ResourceStream object instances, passing each corresponding FileStream instance.

  4. Create a new Dictionary that will hold each ResourceStream with the appropriate identifier.

  5. Add all three ResourceStream object instances to the Dictionary with each corresponding identifier. Each stream will have the same identifier as the management pack resource reference.

  6. Import the management pack with the ImportManagementPack method on the management group.

The following example demonstrates how to add file streams to the management pack when it is imported:

EnterpriseManagementGroup mg = new EnterpriseManagementGroup("localhost");
ManagementPack mp = new ManagementPack("Folder\\RePackaging.Library.xml", mg);

System.IO.FileStream fileStream1 = new System.IO.FileStream("Folder\\Resources\\package_16.png", System.IO.FileMode.Open);
System.IO.FileStream fileStream2 = new System.IO.FileStream("Folder\\Resources\\package_32.png", System.IO.FileMode.Open);
System.IO.FileStream fileStream3 = new System.IO.FileStream("Folder\\Resources\\package_64.png", System.IO.FileMode.Open);

ResourceStream resStream1 = new ResourceStream(mg, fileStream1);
ResourceStream resStream2 = new ResourceStream(mg, fileStream2);
ResourceStream resStream3 = new ResourceStream(mg, fileStream3);

Dictionary<string, ResourceStream> resources = new Dictionary<string, ResourceStream>();
resources.Add("RePackaging.Library.Resources.Images.Package_16", resStream1);
resources.Add("RePackaging.Library.Resources.Images.Package_32", resStream2);
resources.Add("RePackaging.Library.Resources.Images.Package_64", resStream3);

mg.ManagementPacks.ImportManagementPack(mp, resources);

Namespaces

Microsoft.EnterpriseManagement

Microsoft.EnterpriseManagement.Common

Microsoft.EnterpriseManagement.Configuration

System.Collections.Generic

Assemblies

Microsoft.EnterpriseManagement.Core

To add resources to a management pack bundle

  1. Load the management pack to gain reference to the management pack instance.

  2. Create three new FileStream object instances, one for each image file.

  3. Create three new ResourceStream object instances, passing each corresponding FileStream instance.

  4. Create a new ManagementPackBundleWriter object by using the CreateBundleWriter static method from the ManagementPackBundleFactory class.

  5. Create a new bundle with the CreateBundle static method from the ManagementPackBundleFactory class.

  6. Add the management pack to the bundle.

  7. Add each resource stream to the bundle with the AddResourceStream method. Each stream will have the same identifier as the management pack resource reference.

  8. Write the bundle.

The following example demonstrates how to create a bundle with file streams:

EnterpriseManagementGroup mg = new EnterpriseManagementGroup("localhost");
ManagementPack mp = new ManagementPack("Folder\\RePackaging.Library.xml");

System.IO.FileStream fileStream1 = new System.IO.FileStream("Folder\\Resources\\package_16.png", System.IO.FileMode.Open);
System.IO.FileStream fileStream2 = new System.IO.FileStream("Folder\\Resources\\package_32.png", System.IO.FileMode.Open);
System.IO.FileStream fileStream3 = new System.IO.FileStream("Folder\\Resources\\package_64.png", System.IO.FileMode.Open);

ResourceStream resStream1 = new ResourceStream(mg, fileStream1);
ResourceStream resStream2 = new ResourceStream(mg, fileStream2);
ResourceStream resStream3 = new ResourceStream(mg, fileStream3);

ManagementPackBundleWriter bundleWriter = ManagementPackBundleFactory.CreateBundleWriter(".\\");
ManagementPackBundle bundle = ManagementPackBundleFactory.CreateBundle();

bundle.ManagementPacks.Add(mp);

bundle.AddResourceStream(mp, "RePackaging.Library.Resources.Images.Package_16", resStream1, ManagementPackBundleStreamSignature.Empty);
bundle.AddResourceStream(mp, "RePackaging.Library.Resources.Images.Package_32", resStream2, ManagementPackBundleStreamSignature.Empty);
bundle.AddResourceStream(mp, "RePackaging.Library.Resources.Images.Package_64", resStream3, ManagementPackBundleStreamSignature.Empty);

bundleWriter.Write(bundle, "RePackaging.Library");

Namespaces

Microsoft.EnterpriseManagement

Microsoft.EnterpriseManagement.Common

Microsoft.EnterpriseManagement.Configuration

Microsoft.EnterpriseManagement.Packaging

Assemblies

Microsoft.EnterpriseManagement.Core

Microsoft.EnterpriseManagement.Packaging

 

Community Additions

ADD
Show: