Maintaining Contacts and Groups
When a subscription to the self-published contact list is in a Subscribed state, an application can add another contact or group to the existing contact list, remove a contact or group from the contact list, and updating a contact or group in the list. These operations amount to publications of contacts or groups category instances. In UCMA, they are exposed by the related methods of the ContactGroupServices object.
Add a Contact to Contact List
Adding a contact to an existing contact list is an asynchronous operation and involves calls to BeginAddContact/EndAddContact on ContactGroupServices object. The following code example shows how this is implemented in a UCMA application.
public void AddContact(string contactUri, string contactData, string contactExtension, string contactName, int containerId)
{
if (_cgServices.CurrentState == CollaborationSubscriptionState.Subscribed)
{
ContactAddOptions options = new ContactAddOptions();
options.ContactData = contactData;
options.ContactExtension = contactExtension;
options.ContactName = contactName;
options.ContainerId = containerId;
_cgServices.BeginAddContact(contactUri, options, CallbackOnBeginAddContact, _cgServices);
}
}
void CallbackOnBeginAddContact(IAsyncResult result)
{
if (_cgServices != result as ContactGroupServices)
return;
try
{
_cgServices.EndAddContact(result);
}
catch (Exception ex)
{
MessageBox.Show("Failed to AddContact: " + ex.Message);
}
}
Remove a Contact from Contact List
Removing a contact from an existing contact list is an asynchronous operation and involves calls to BeginDeleteContact/EndDeleteContact on ContactGroupServices object. The following code example shows how this is implemented in a UCMA application.
public void DeleteContact(string contactUri)
{
if (_cgServices.CurrentState == CollaborationSubscriptionState.Subscribed)
{
_cgServices.BeginDeleteContact(
contactUri, CallbackOnBeginDeleteContact, contactUri);
}
}
void CallbackOnBeginDeleteContact(IAsyncResult result)
{
try
{
if (result.IsCompleted)
{
// Stop subscription to the presence of the now removed contact
string sipUri = result.AsyncState as string;
this.UnsubscribeToContactsPresence(new string[] {sipUri} );
// Complete the operation to delete the contact
_cgServices.EndDeleteContact(result);
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to DeleteContact: " + ex.Message);
}
}
Updating a Contact in Contact List
Updating a contact in an existing contact list is an asynchronous operation and involves calls to BeginUpdateContact/EndUpdateContact on ContactGroupServices object. The following code example shows how this is implemented in a UCMA application.
public void UpdateContact(Contact contact)
{
if (_cgServices.CurrentState == CollaborationSubscriptionState.Subscribed)
{
_cgServices.BeginUpdateContact(
contact, CallbackOnBeginUpdateContact, _cgServices);
}
}
void CallbackOnBeginUpdateContact(IAsyncResult result)
{
if (_cgServices != result as ContactGroupServices)
return;
try
{
_cgServices.EndUpdateContact(result);
}
catch (Exception ex)
{
MessageBox.Show("Failed to UpdateContact: " + ex.Message);
}
}
Add, Delete and Update a Group
Adding, deleting, and updating a group in an existing contact list are asynchronous operations and involve calls to BeginAddGroup/EndAddgroup, BeginDeleteGroup/EndDeleteGroup and BeginUpdateGroup/EndUpdateGroup on ContactGroupServices object. The following code example shows how these are implemented in a UCMA application.
public void AddGroup(string groupName, string groupData)
{
if (_cgServices.CurrentState == CollaborationSubscriptionState.Subscribed)
{
_cgServices.EndAddGroup(
_cgServices.BeginAddGroup(groupName, groupData, null, null));
}
}
public void DeleteGroup(int groupId, string groupData)
{
if (_cgServices.CurrentState == CollaborationSubscriptionState.Subscribed)
{
_cgServices.EndDeleteGroup(
_cgServices.BeginDeleteGroup(groupId, null, null));
}
}
public void UpdateGroup(Group group)
{
if (_cgServices.CurrentState == CollaborationSubscriptionState.Subscribed)
{
_cgServices.EndUpdateGroup(
_cgServices.BeginUpdateGroup(group, null, null));
}
}
For the purpose of illustrating the features specific to managing contact groups, the code example above simplified the asynchronous patterns into synchronous calls. You can follow the contact-managing examples above to revert these calls to their asynchronous counterparts.