How To Add Users and Groups to MTS Roles Programmatically (241345)



The information in this article applies to:

  • Microsoft Transaction Server 2.0

This article was previously published under Q241345

SUMMARY

Through the use of the administration objects in MTSAdmin, developers and administrators can write their own Microsoft Management Console (MMC) snap-ins and add-ins, Microsoft Visual Basic applications, ActiveX controls, and Active Server Pages (ASP) pages to build a user interface that is more effective for common tasks. This article provides some skeletal Visual Basic code that adds a member to a certain role is given.

MORE INFORMATION

Programmatic administration of Microsoft Transaction Server is done through objects found in the MTSAdmin type library. The MTSAdmin type library appears as "MTS 2.0 Admin Type Library" in the Visual Basic references dialog box. These objects expose a hierarchical "tree" structure that is represented through CatalogCollection and CatalogObject objects.

To add a member to a role, you must:
  1. Connect to a catalog on a certain computer.
  2. Traverse the packages to find the required one.
  3. Traverse in turn the roles for the package to find the required one.
  4. Create a new CatalogObject for the new member.
  5. Assign the member name to the appropriate CatalogObject property.
The following Visual Basic code does all this in a single subroutine call:
Private Sub AddMemberToRole(Catalog As String, Package As String, Role As String, NewMember As String)

    Dim oCatalog As MTSAdmin.Catalog
    
    
    Set oCatalog = New MTSAdmin.Catalog
    oCatalog.Connect Catalog
    
    Dim oPackages As MTSAdmin.CatalogCollection
    Set oPackages = oCatalog.GetCollection("Packages")
    
    oPackages.Populate
    Dim oPackage As MTSAdmin.CatalogObject
    
    Dim bFoundPackage As Boolean
    
    For Each oPackage In oPackages
        
        If oPackage.Name = Package Then
            bFoundPackage = True
        
            
            Dim oRoles As CatalogCollection
            Dim oRole As CatalogObject
            
            Dim bFoundRole As Boolean
            
            Set oRoles = oPackages.GetCollection("RolesInPackage", oPackage.Key)
            oRoles.Populate
            For Each oRole In oRoles
                If oRole.Name = Role Then
                    Debug.Print oRole.Name
                    bFoundRole = True
                    Dim oUsersInRole As MTSAdmin.CatalogCollection
                    Set oUsersInRole = oRoles.GetCollection("UsersInRole", oRole.Key)
                    Dim oNewMember As CatalogObject
                    Set oNewMember = oUsersInRole.Add
                    
                    oNewMember.Value("User") = CStr(NewMember)
                    
                    oUsersInRole.SaveChanges
                    
                    Set oUsersInRole = Nothing
                    
                    Set oNewMember = Nothing
                  
                    Exit For
                End If
            Next
            If Not bFoundRole Then Err.Raise vbObjectError + 101, "AddMemberToRole", "Role '" & Role & "' of package '" & Package & "' was not found on '" & Catalog & "'"
            
            Set oRoles = Nothing
          
            Exit For
        End If
    Next
    If Not bFoundPackage Then Err.Raise vbObjectError + 100, "AddMemberToRole", "Package '" & Package & "' was not found on '" & Catalog & "'"
    
    
    Set oPackages = Nothing
    Set oCatalog = Nothing

End Sub
				
The use of the CatalogObject and CatalogCollection objects is simple, but you should check the documentation to see which collections and properties (identified by strings such as "RolesInPackage", "UsersInRole", and "User") are supported by each member in the hierarchy.

You can call the function as follows:
AddMemberToRole "My Computer", "Bank Sample", "Managers", "DOMAIN\Account"
'Please note 'Account' can either be a user or group name
				
To be able to alter MTS/COM+ configuration, the script must run under an identity included in the administrator's role for the "System" package/application.

Note for Microsoft Visual C++ developers: If you are using Visual C++, the code may differ significantly because you have access to the PopulateByKey and PopulateByQuery methods of the CatalogCollection interface, thus eliminating the need to loop through all the items. Please refer to the documentation found in MSDN online for more information on these methods:

Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbCodeSnippet kbhowto KB241345