Partager via


How to Add Properties to a Class

Applies To: System Center 2012 - Service Manager

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

Properties are part of a management pack class and store pieces of information related to the class. Though, a class does not need to have any properties defined. The type of data the property will hold can be changed. The default type of a property is an integer. This can be changed by altering the value of the Type property. The type of property typically corresponds to a Microsoft .NET type. However, three types do not: binary, richtext, and enum.

A management pack property can be designated as a key property by setting the Key property to true. When an object instance is created and the class definition contains one or more key properties, they all must have values specified before the instance can be committed. The values of key properties must be unique across all class instances.

To add a property to a management pack class

  1. Get reference to a management pack.

  2. Get reference to a management pack class.

  3. Create a new instance of the ManagementPackProperty class using the management pack class object.

  4. Set the DisplayName and Description properties.

  5. Set the Type property to the appropriate value.

  6. Add the property instance to the PropertyCollection property of the class.

  7. Set the management pack class Status property to PendingUpdate.

  8. Call the AcceptChanges method on the management pack.

Example

The following example demonstrates adding a property to a management pack class:

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

ManagementPackProperty propRequester = new ManagementPackProperty(mpClass, "Requester");
ManagementPackProperty propSoftwareTitle = new ManagementPackProperty(mpClass, "SoftwareTitle");
ManagementPackProperty propBitsURI = new ManagementPackProperty(mpClass, "BitsURI");

propRequester.DisplayName = "Requester";
propSoftwareTitle.DisplayName = "Software Title";
propBitsURI.DisplayName = "Software Location";

propRequester.Description = "The name of the person requesting the software.";
propSoftwareTitle.Description = "The title of the software.";
propBitsURI.Description = "The URI of the software.";

propRequester.Type = ManagementPackEntityPropertyTypes.@string;
propSoftwareTitle.Type = ManagementPackEntityPropertyTypes.@string;
propBitsURI.Type = ManagementPackEntityPropertyTypes.@string;

mpClass.PropertyCollection.Add(propRequester);
mpClass.PropertyCollection.Add(propSoftwareTitle);
mpClass.PropertyCollection.Add(propBitsURI);

mpClass.Status = ManagementPackElementStatus.PendingUpdate;

mp.AcceptChanges();

The following example demonstrates what the management pack XML elements should look like:

<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>
</ManagementPack>

Compiling the Code

Namespaces

Microsoft.EnterpriseManagement

Microsoft.EnterpriseManagement.Configuration

Assemblies

Microsoft.EnterpriseManagement.Core

See Also

Tasks

How to Read a Class From a Management Pack
How to Create a Class
How to Update Existing Classes
How to Reference Enumerations

Reference

ManagementPackProperty
ManagementPackEntityPropertyTypes
ManagementPackClass
ManagementPack

Other Resources

Scenario: Managing Classes