How to Add a Configuration Manager Context Qualifier by Using Managed Code
Updated: November 1, 2013
Applies To: System Center 2012 Configuration Manager, System Center 2012 Configuration Manager SP1, System Center 2012 R2 Configuration Manager
In System Center 2012 R2 Configuration Manager, to add a context qualifier by using the managed SMS Provider, use the P:Microsoft.ConfigurationManagement.ManagementProvider.ConnectionManagerBase.Context property which is a Dictionary object that holds context qualifiers.
Typically you will add your application name to the ApplicationName context qualifier, along with the computer name (MachineName) and Locale identifier (LocaleID).
To add Configuration Manager context qualifier
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
Get the SmsNamedValuesDictionary object from the WqlConnectionManager object that you get from step 1.
Add the context qualifiers as required.
Example
The following C# example first adds a number of context qualifiers to a WQLConnectionManager object Context dictionary property. It then displays a list of the context qualifiers in dictionary object.
Note |
|---|
WqlConnectionManager derives from ConnectionManagerBase. |
In the example, the LocaleID context qualifier is hard-coded to English (U.S.). If you need the locale for non-U.S. installations, you can get it from the SMS_Identification Server WMI Class LocaleID property.
For information about calling the sample code, see Calling Configuration Manager Code Snippets.
public void AddContextQualifiers(WqlConnectionManager connection)
{
try
{
connection.Context.Add("ApplicationName", "My application name");
connection.Context.Add("MachineName","Computername");
connection.Context.Add("LocaleID", @"MS\1033");
foreach (KeyValuePair<string, object> namedValue in connection.Context)
{
Console.WriteLine(namedValue.Key);
Console.WriteLine(namedValue.Value);
Console.WriteLine();
}
}
catch (SmsException e)
{
Console.WriteLine("Failed to add context qualifier : " + e.Message);
}
}
Compiling the Code
System
System.Collections.Generic
System.ComponentModel
Microsoft.ConfigurationManagement.ManagementProvider
Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine
microsoft.configurationmanagement.managementprovider
adminui.wqlqueryengine
Robust Programming
The Configuration Manager exceptions that can be raised are T:Microsoft.ConfigurationManagement.ManagementProvider.SmsConnectionException and T:Microsoft.ConfigurationManagement.ManagementProvider.SmsQueryException. These can be caught together with T:Microsoft.ConfigurationManagement.ManagementProvider.SmsException.

The example method has the following parameters:
Parameter
Type
Description
connection
WqlConnectionManager
A valid connection to the SMS Provider.