To read a lazy property from a System Center 2012 R2 Configuration Manager 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 System Center 2012 R2 Configuration Manager 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.

  Copy Code
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