IResultObject Lazy Properties

IResultObject Lazy Properties

Overview

Some SCCM WMI classes make use of lazy properties. These are properties that are not returned from WqlConnectionManager.ExecuteQuery() by default in order to reduce the time taken for large queries to be processed.

Note

When using IResultObject.Get() or WqlConnectionManager.GetInstance() the lazy properties are returned.

Here is an extract of the SMS_Collection class description on MSDN:

CollectionID
Data type: String
Access type: Read-only
Qualifiers: [key, read]
Unique auto-generated ID containing eight characters. The default value is “”.
The format of the collection ID is the site code that created the collection followed by a five-digit hexadecimal serial number, for example, “JAX0002C”. The default Configuration Manager collections, created during the installation, use the “SMS” prefix, for example, “SMS00001”.

CollectionRules
Data type: SMS_CollectionRule Array
Access type: Read/Write
Qualifiers: [lazy]

The Qualifiers entry for each property is important as it shows the primary key (‘key‘) for the class and also which of the properties are lazy properties (‘lazy‘).

Lazy properties are either set to null or blank after retrieving using ExecuteQuery(). If you are only reading properties of a class and you don’t require access to the lazy properties then there is no need to call Get() on them – and it is more efficient when working with large query sets. But you must call Get() or use GetInstance() in the following scenarios:

  • You need to read the lazy property.
  • You intend to write the object back to SCCM with IResultObject.Put()

Writing an object that contains lazy properties back to SCCM without first retrieving those lazy properties will corrupt the object.

Examples and Downloads

The examples on this page can be downloaded from the GitHub repo:

IResultObject – Read Lazy Properties from SMS_Collection

This example uses ExecuteQuery to access a collection and then the Get method to get lazy properties CollectionRules.

The rules are disposed using an extension method. See IResultObject Disposal / GetArrayItems for details.

public void LazyPropertyFromQuery(WqlConnectionManager connection)  
{  
    try  
    {  
        // Query the All Systems collection (this has two default rules)
        using (IResultObject collections = connection.QueryProcessor.ExecuteQuery("Select * from SMS_Collection Where CollectionID='SMS00001'"))
        {
            foreach (IResultObject collection in collections)
            {
                // Show collection name
                Console.WriteLine(collection["Name"].StringValue);

                // Get the collection object and lazy properties.  
                collection.Get();

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

                foreach (IResultObject rule in rules)
                {
                    // Display rule names.  
                    Console.WriteLine("Rule name: " + rule["RuleName"].StringValue);
                }
        
               // Dispose the rules using our List extension method
               rules.DisposeItems();
            }
        }
    }  
    catch (SmsQueryException ex)  
    {  
        Console.WriteLine("Failed to get collection. Error: " + ex.Message);  
        throw;  
    }  
}