| title | Lazy Properties by Using Managed Code |
|---|---|
| description | To read a lazy property from a Configuration Manager object returned in a query, you get the object instance, which retrieves any lazy object properties from the SMS Provider. |
| ms.date | 09/20/2016 |
| ms.subservice | sdk |
| ms.topic | how-to |
| ms.collection | tier3 |
To read a lazy property from a 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.
-
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.
-
Use QueryProcessor object to query Configuration Manager objects.
-
Iterate through the query results.
-
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.
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. |
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
The Configuration Manager exceptions that can be raised are SmsConnectionException and SmsQueryException. These can be caught together with SmsException.
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 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