Share via


Getting a Subscriber's Devices and Subscriptions

The Subscriber class provides the GetSubscriptions and GetDevices methods to provide access to subscriber device and subscription records related to a specified subscriber.

Use the GetSubscriptions method of the Subscriber class to return a SubscriptionEnumeration object that represents the collection of all the subscriptions of one subscription class in one application for the specified subscriber.

Use the GetDevices method of the Subscriber class to return a SubscriberDeviceEnumeration object that represents the collection of all the devices for the specified subscriber.

Managed Code Example: Return Subscriptions

The following code example shows how to use a Subscriber object in managed code to return the value for a subscription field for all of a subscriber's subscriptions in a subscription class.

string instanceName = "Tutorial";
string applicationName = "Weather";
string subscriptionClassName = "WeatherCity";
string subscriptionFieldName = "City";

// Create the NSInstance object.
NSInstance testInstance = new NSInstance(instanceName);

// Create the Subscriber object.
Subscriber testSubscriber = new Subscriber(testInstance);

// Set the subscriber ID so that the
// correct records are retrieved. 
testSubscriber.SubscriberId = "TestUser1";

// Create the NSApplication object.
NSApplication testApplication =
    new NSApplication(testInstance, applicationName);

// Retrieve the subscriber's subscriptions.
SubscriptionEnumeration testSubscriptionEnumeration =
    testSubscriber.GetSubscriptions
    (testApplication, subscriptionClassName);

foreach (Subscription singleSub in testSubscriptionEnumeration)
{
    Console.WriteLine(singleSub[subscriptionFieldName].ToString());
}
Console.ReadLine();

Managed Code Example: Return Devices

The following example shows how to use a Subscriber object in managed code to return all devices belonging to a single subscriber.

string instanceName = "Tutorial";

// Create the NSInstance object.
NSInstance testInstance = new NSInstance(instanceName);

// Create the Subscriber object.
Subscriber testSubscriber = new Subscriber(testInstance);

// Set the subscriber ID so that the
// correct records are retrieved. 
testSubscriber.SubscriberId = "TestUser1";

// Retrieve the subscriber's devices.
SubscriberDeviceEnumeration testSubscriberDeviceEnumeration =
    testSubscriber.GetDevices();

// Print each device to the console.
foreach (SubscriberDevice singleSubDevice in
    testSubscriberDeviceEnumeration)
{
    Console.WriteLine(singleSubDevice.DeviceName);
}
Console.ReadLine();

COM Interop Example: Return Subscriptions

The following VBScript code example shows how to use a Subscriber object in managed code to return the value for a subscription field for all of a subscriber's subscriptions in a subscription class.

Dim testInstance, testSubscriber, _
    testApplication, testSubscriptionEnumeration

const instanceName = "Tutorial"
const applicationName = "Weather"
const subscriptionClassName = "WeatherCity"
const subscriptionFieldName = "City"

' Create the NSInstance object.
set testInstance = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.NSInstance")
testInstance.Initialize instanceName

' Create the Subscriber object.
set testSubscriber = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.Subscriber")
testSubscriber.Initialize (testInstance)

' Create the NSApplication object.
set testApplication = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.NSApplication")
testApplication.Initialize (testInstance), applicationName

' Set the subscriber ID
testSubscriber.SubscriberID = "TestUser2"

' Retrieve a subscriber's subscriptions.
set testSubscriptionEnumeration = _ 
    testSubscriber.GetSubscriptions((testApplication), _ 
      subscriptionClassName)

' Print field value
for each subscription in testSubscriptionEnumeration
        Wscript.Echo "Field Value", _ 
            subscription.GetFieldValue(subscriptionFieldName)
next

wscript.echo "Done!"

COM Interop Example: Return Devices

The following VBScript example shows how to use a Subscriber object in managed code to return all devices belonging to a single subscriber.

Dim testInstance, testSubscriber, testSubscriberDeviceEnumeration
const instanceName = "Tutorial"

' Create the NSInstance object.
set testInstance = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.NSInstance")
testInstance.Initialize instanceName

' Create the Subscriber object.
set testSubscriber = WScript.CreateObject( _ 
    "Microsoft.SqlServer.NotificationServices.Subscriber")
testSubscriber.Initialize (testInstance)

' Set the subscriber ID so that the
testSubscriber.SubscriberId = "TestUser2"

' Retrieve the subscriber's devices.
set testSubscriberDeviceEnumeration = testSubscriber.GetDevices

for each singleSubDevice in testSubscriberDeviceEnumeration
    Wscript.Echo "Device Name:", singleSubDevice.DeviceName
next

wscript.echo "Done!"

See Also

Concepts

Creating a Subscriber Object
Adding a Subscriber Record
Updating a Subscriber Record
Deleting a Subscriber Record
Deleting Related Subscription Information

Other Resources

NSSubscriberView

Help and Information

Getting SQL Server 2005 Assistance