How to Create a Configuration Manager Object by Using Managed Code

To create a Configuration Manager object by using the managed SMS Provider, use WqlConnectionManager.CreateInstance method. The ConnectionManagerBase.CreateInstance method takes the required object type as a string parameter and returns an IResultObject object that is used to populate the new object. The IResultObject.Put method must be called to submit the object to the SMS Provider.

To create a Configuration Manager object

  1. Set up a connection to the SMS Provider. For more information, see SMS Provider fundamentals.

  2. Using the WqlConnectionManager connection object you obtain in step one, call [CreateInstance to create the required the WMI object, and receive its IResultObject object instance.

  3. Populate the IResultObject properties.

  4. Commit the IResultObject to the SMS Provider.

Example

The following example demonstrates how to create and then populate a new Configuration Manager package (SMS_Package).

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

public void CreatePackage(WqlConnectionManager connection)  
{  
    try  
    {  
        IResultObject package = connection.CreateInstance("SMS_Package");  
        package["Name"].StringValue = "Test Package";  
        package["Description"].StringValue = "A test package";  
        package["PkgSourcePath"].StringValue = @"c:\Package Source";  

        package.Put();  
    }  

    catch (SmsException ex)  
    {  
        Console.WriteLine("Failed to create package. Error: " + ex.Message);  
        throw;  
    }  
}  

This example method has the following parameters:

Parameter Type Description
connection Managed: WqlConnectionManager A valid connection to the SMS Provider.

Compiling the Code

Namespaces

System

System.Collections.Generic

System.ComponentModel

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

Robust Programming

The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.

See Also

Objects overview Configuration Manager Lazy Properties
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to a Configuration Manager Provider using Managed Code
How to Modify a Configuration Manager Object by Using Managed Code
How to Perform an Asynchronous Configuration Manager Query by Using Managed Code
How to Perform a Synchronous Configuration Manager Query by Using Managed Code
How to Read a Configuration Manager Object by Using Managed Code
How to Read Lazy Properties by Using Managed Code