Share via


How to Read Lazy Properties by Using Managed Code

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

To read a lazy property from a Configuration Manager 2007 object returned in a query, you get the object instance, which retrieves any lazy object properties from the SMS Provider.

Note

If you know the full path to the WMI object, a call to the GetInstance method returns the WMI object along with any lazy properties. For more information, see How to Read a Configuration Manager Object by Using Managed Code.

For more information, see Configuration Manager Lazy Properties.

To read lazy properties

  1. Set up a connection to the SMS Provider. For more information, see How to Connect to an SMS Provider in Configuration Manager by Using Managed Code.

  2. Use QueryProcessor object to query Configuration Manager 2007 objects.

  3. Iterate through the query results.

  4. Using the WqlConnectionManager you obtain in step one, call GetInstance to get the IResultObject object for each queried object that you want to get lazy properties from.

Example

The following C# code example queries for all SMS_Collection objects and then displays rule names obtained from the CollectionRules lazy property.

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

public void ReadLazyProperty(WqlConnectionManager connection)
{
    try
    {
        // Query all collections.
        IResultObject collections = connection.QueryProcessor.ExecuteQuery("Select * from SMS_Collection");
        foreach (IResultObject collection in collections)
        {
            // Get the collection object and lazy properties.
            collection.Get();

            Console.WriteLine(collection["Name"].StringValue);

            // Get the rules.
            List<IResultObject> rules = collection.GetArrayItems("CollectionRules");
            if (rules.Count == 0)
            {
                Console.WriteLine("No rules");
                Console.WriteLine();
                continue;
            }

            foreach (IResultObject rule in rules)
            {
                // Display rule names.
                Console.WriteLine("Rule name: " + rule["RuleName"].StringValue);
            }

            Console.WriteLine();
        }
    }
    catch (SmsQueryException ex)
    {
        Console.WriteLine("Failed to get collection. Error: " + ex.Message);
        throw;
    }
}

This example method has the following parameters:

Parameter Type Description

connection

  • 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

Concepts

About Configuration Manager Objects
Configuration Manager Lazy Properties
How to Call a Configuration Manager Object Class Method by Using Managed Code
How to Connect to an SMS Provider in Configuration Manager by Using Managed Code
How to Create a Configuration Manager Object by 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 Use Configuration Manager Objects with Managed Code