Retrieve Presence Information

The following code example demonstrates how to retrieve the presence data and the local presence information. The operations in the Initialize RTC, Set Presence Information, and Roaming Events code examples must be performed before using this example.

Note  This example does not contain error checking or releases appropriate for real code.

C++ Code Example

HRESULT ProcessPresenceInformation(IRTCBuddy2 *pBuddy)
{
    HRESULT hr = S_OK;
    BSTR bstrBuddyDataNameSpace = NULL;
    BSTR bstrBuddyData = NULL;

    if (pBuddy == NULL)
        return E_INVALIDARG;

    if (bstrBuddyData)
        ::SysFreeString(bstrBuddyData);

    // Get the display name published by this buddy.
    hr = pBuddy->get_PresenceProperty(RTCPP_DISPLAY_NAME, 
                                      &bstrBuddyData);

    // If (hr != S_OK), process the error here.

    // Update the user interface or other structures
    // with the Buddy's display name. 

    if (bstrBuddyData)
        ::SysFreeString(bstrBuddyData);

    // Get the e-mail published by this buddy.
    hr = pBuddy->get_PresenceProperty(RTCPP_EMAIL, 
                                      &bstrBuddyData);	

    // If (hr != S_OK), process the error here.

    // Update the user interface or other structures
    // with the Buddy's e-mail address. 

    if (bstrBuddyData)
        ::SysFreeString(bstrBuddyData);

    // Get the phone number published by this buddy.
    hr = pBuddy->get_PresenceProperty(RTCPP_PHONENUMER, 
                                      &bsrtBuddyData);

    // If (hr != S_OK), process the error here.

    // Update the user interface or other structures
    // with the Buddy's phone number. 

    if (bstrBuddyData)
        ::SysFreeString(bstrBuddyData);

    // Get the IRTCEnumPresenceDevices interface.
    IRTCEnumPresenceDevices* ppEnumDevices = NULL;
    hr = pBuddy->EnumeratePresenceDevices(&ppEnumDevices);
	
    // If (hr != S_OK), process the error here.
		
    ULONG celtFetched = 0;
    IRTCPresenceDevice *pDevice = NULL;

    // Get all the presence devices for this buddy.
    for (; (hr = ppEnumDevices->Next(1, &pDevice, &celtFetched)) == S_OK;)
    {
        BSTR bstrDeviceNotes = NULL;
        hr = pDevice->get_Notes(&bstrDeviceNotes);

         // If (hr != S_OK), process the error here.

        // Update the user interface or other structures
        // with the Buddy's device notes. 
		
        if (bstrDeviceNotes)
           ::SysFreeString(bstrDeviceNotes);

        BSTR bstrDeviceData = NULL;
        hr = pDevice->GetPresenceData(&bstrBuddyNameSpace, 
                                      &bstrDeviceData);

        // If (hr != S_OK), process the error here.

        // Update the user interface or other structures
        // with the Buddy's device data. 

        // Get the display name published by this device.
        hr = pDevice->get_PresenceProperty(RTCPP_DISPLAY_NAME, 
                                           &bstrDeviceData);

        // If (hr != S_OK), process the error here.

        // Update the user interface or other structures
        // with the Buddy device display name. 

        if (bstrDeviceData)
           ::SysFreeString(bstrDeviceData);

        // Get the e-mail published by this device.
        hr = pDevice->get_PresenceProperty(RTCPP_EMAIL, 
                                           &bstrDeviceData);	

        // If (hr != S_OK), process the error here.

        // Update the user interface or other structures
        // with the Buddy device e-mail address. 

        if (bstrDeviceData)
           ::SysFreeString(bstrDeviceData);
	
        // Get the phone number published by this device
        hr = pDevice->get_PresenceProperty(RTCPP_PHONENUMER, 
                                           &bstrDeviceData);

        // If (hr != S_OK), process the error here.

        // Update the user interface or other structures
        // with the Buddy device phone number. 

        if (bstrDeviceData)
           ::SysFreeString(bstrDeviceData);

        // Get the device name published by this device.
        hr = pDevice->get_PresenceProperty(RTCPP_DEVICE_NAME, 
                                           &bstrDeviceData);

        // If (hr != S_OK), process the error here.

        // Update the user interface or other structures
        // with the Buddy device name. 

        if (bstrDeviceData)
           ::SysFreeString(bstrDeviceData);

        if (pDevice)
           pDevice->Release();
    }

    if (ppEnumDevices)
        ppEnumDevices->Release();

    return hr;
}

Visual Basic Code Example

'
' Reading from a Buddy
'
Dim pBuddy As IRTCBuddy2
Dim pDevice As IRTCPresenceDevice
Dim szDisplayName As String, szPhoneNumber As String, szEmailAddress As String, szDeviceName As String
Dim szNamespace As String, szData As String

' Add Code: Retrieve the pBuddy pointer.

If (pBuddy Is Nothing) Then
    ' Handle error 
End If

' Print Information about the Buddy.

' Get the display name published by this buddy.
szDisplayName = pBuddy.PresenceProperty(RTCPP_DISPLAY_NAME)

' Get the e-mail published by this buddy.
szEmailAddress = pBuddy.PresenceProperty(RTCPP_EMAIL)

' Get the phone number published by this buddy.
szPhoneNumber = pBuddy.PresenceProperty(RTCPP_PHONENUMBER)


For lCount = 1 To pBuddy2.PresenceDevices.Count

    Set pDevice = pBuddy2.PresenceDevices.Item(lCount)

    lStatus = pDevice.Status
    szNotes = pDevice.Notes

    ' Get the display name published by this device.
    szDisplayName = pDevice.PresenceProperty(RTCPP_DISPLAYNAME)

    ' Get the phone number published by this device.
    szPhoneNumber = pDevice.PresenceProperty(RTCPP_PHONENUMBER)

    ' Get the e-mail published by this device.
    szEmailAddress = pDevice.PresenceProperty(RTCPP_EMAIL)

    ' Get the device name published by this device.
    szDeviceName = pDevice.PresenceProperty(RTCPP_DEVICE_NAME)

    ' Get extended presence information. (This can include GPS, etc.)
    Call pDevice.GetPresenceData(szNamespace, szData)

    ' Process information. (Update the user interface or other structures.)

    Set pDevice = Nothing
Next