Share via


How to Create a Relationship

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.]

The relationship object allows you to relate two management pack class object instances to each other. When designing a relationship, the source, target, and type of relationship must be specified. The source and target of the relationship are management pack class types, and are subject to specific rules based on the type of relationship. The type of relationship is specified by setting the Base property to an existing relationship type from the System.Library management pack. For more information, see Modeling Overview.

To create a basic relationship

  1. Get reference to two different management pack class objects.

  2. Create a new instance of the ManagementPackRelationship class using the management pack that will store this relationship.

  3. Set the Source and Target properties.

    This is done by creating new instances of the ManagementPackRelationshipEndpoint class, and providing the relationship object you are creating to the constructor.

  4. For both the Source and Target properties, that had the relationship endpoint instance created, set the Type properties to the appropriate management pack class objects.

  5. Set the Base property to the desired type of relationship.

  6. Call the AcceptChanges method on the management pack.

Example

The following example demonstrates creating a relationship between the RePackaging.Library!RePackaging.Request and System.Software.Library!System.SoftwareItem management pack classes:

EnterpriseManagementGroup mg = new EnterpriseManagementGroup("localhost");
ManagementPack mp = new ManagementPack("Folder\\RePackaging.Library.xml", mg);
ManagementPack mpSoftware = mg.ManagementPacks.GetManagementPack("System.Software.Library", "9396306c2be7fcc4", new Version());
ManagementPack mpSystem = mg.ManagementPacks.GetManagementPack("System.Library", "9396306c2be7fcc4", new Version());
ManagementPackClass requestClass = mp.GetClass("RePackaging.Request");
ManagementPackClass softwareClass = mpSoftware.GetClass("System.SoftwareItem");
ManagementPackRelationship referenceRelationship = mpSystem.GetRelationship("System.Reference");

ManagementPackRelationship softwareToRequest = new ManagementPackRelationship(mp, "Relationship.SoftwareToRequest", ManagementPackAccessibility.Public);

softwareToRequest.Source = new ManagementPackRelationshipEndpoint(softwareToRequest, "Request");
softwareToRequest.Source.Type = requestClass;

softwareToRequest.Target = new ManagementPackRelationshipEndpoint(softwareToRequest, "Software");
softwareToRequest.Target.Type = softwareClass;

softwareToRequest.Base = referenceRelationship;

mp.AcceptChanges();

The following example shows the management pack XML:

<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">
...
        </ClassType>
      </ClassTypes>
      <RelationshipTypes>
        <RelationshipType ID="Relationship.SoftwareToRequest" Base="System!System.Reference" Abstract="false" Accessibility="Public">
          <Source ID="Request" Type="RePackaging.Request" />
          <Target ID="Software" Type="Software!System.SoftwareItem" />
        </RelationshipType>
      </RelationshipTypes>
...
    </EntityTypes>
  </TypeDefinitions>
...
</ManagementPack>

Compiling the Code

Namespaces

Microsoft.EnterpriseManagement

Microsoft.EnterpriseManagement.Configuration

System

Assemblies

Microsoft.EnterpriseManagement.Core

System

See Also

Tasks

How to Establish a Relationship

Reference

RelationshipTypes
ManagementPackRelationship
ManagementPackRelationshipEndpoint
ManagementPackClass
ManagementPack

Other Resources

Scenario: Designing Relationships