Share via


Create a Buddy Group and Add Buddies

The following code example demonstrates how to create a Buddy Group and add buddies to the group. The operations in the Initialize RTC code example must be performed before using this example.

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

C++ Code Example

// Set the group name and group data.  
HRESULT  hr = S_OK;
BSTR  bstrGroupName = ::SysAllocString(L"Co-workers");
BSTR  bstrData = ::SysAllocString(L"Sample Data, for example, URL for group");
IRTCProfile  *pProfile; // The profile used to connect to your server
long  lFlags = 0; // Only used if you want to fail on redirect
IRTCBuddyGroup  *pGroup = NULL;
IRTCClient2  *pClient =  // Set this to your main client instance.
IRTCClientPresence2  *pPresence2 = NULL;

IRTCBuddy *pBuddyCoWorker1 = NULL;
IRTCBuddy *pBuddyCoWorker2 = NULL;

// Query for the IRTCClientPresence2 interface.
hr = pClient->QueryInterface(IID_IRTCClientPresence2, 
                             (LPVOID *) &pPresence2);

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

BSTR bstrCoWorker1 = ::SysAllocString(L"coworker1uri@example.com");
BSTR bstrCoWorker2 = ::SysAllocString(L"coworker2uri@example.com");

// If ( bstrCoWorker1 == NULL ), process the error here. 
// If ( bstrCoWorker2 == NULL ), process the error here. 

hr = pPresence2->get_Buddy(bstrCoWorker1, 
                           &pBuddyCoWorker1);

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

hr = pPresence2->get_Buddy(bstrCoWorker2, 
                           &pBuddyCoWorker2);

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

// Create the group.
hr = pPresence2->AddGroup(bstrGroupName, 
                          bstrData, 
                          pProfile, 
                          lFlags, 
                          &pGroup);

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

// The group event should fire with an 
// RTCGET_GROUP_ADD event type.

// Add buddies to the group.
hr = pGroup->AddBuddy(pBuddyCoWorker1);

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

// The group event should fire with an 
// RTCGET_GROUP_BUDDY_ADD event type.

hr = pGroup->AddBuddy(pBuddyCoWorker2);

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

// The group event should fire with an
// RTCGET_GROUP_BUDDY_ADD event type.

// Release all pointers.
if (pBuddyCoWorker1)
    pBuddyCoWorker1->Release();

if (pBuddyCoWorker2)
    pBuddyCoWorker2->Release();

if (pGroup)
    pGroup->Release();

if (pPresence2)
    pPresence2->Release(); 

if (bstrCoWorker1)
    ::SysFreeString(bstrCoWorker1); 

if (bstrCoWorker2)
    ::SysFreeString(bstrCoWorker1);  

Visual Basic Code Example

Dim pPresence2 As IRTCClientPresence2
Dim pProfile As IRTCProfile2
Dim pGroup As IRTCBuddyGroup

' Add code here to set the pPresence2 and pProfile 
' pointers to valid objects.

Dim pBuddyCoWorker1 As IRTCBuddy
Dim pBuddyCoWorker2 As IRTCBuddy

' Get the buddies to add to the group.
pBuddyCoWorker1 = pPresence2.Buddy("coworker1URI@example.com")
pBuddyCoWorker2 = pPresence2.Buddy("coworker2URI@example.com")

' Create the group.
Set pGroup = pPresence2.AddGroup("Co-workers", _
                                 "Sample data, for example, URL for this group", _
                                 pProfile, _
                                 0)

' Note: The group event should fire 
' with an event of type RTCGET_GROUP_ADD.

'
' Add Buddies to the Group.
'
Call pGroup.AddBuddy(pBuddyCoWorker1)
' Note: The group event should fire 
' with an event of type RTCGET_GROUP_ADD. 

Call pGroup.AddBuddy(pBuddyCoWorker2)
' Note: The group event should fire 
' with an event of type RTCGET_GROUP_BUDDY_ADD.

' Remove Buddies from the Group.
Call pGroup.RemoveBuddy(pBuddyCoWorker1)
' Note: The group event should fire 
' with an event of type RTCGET_GROUP_BUDDY_REMOVE.