Table of Contents

Managing groups

Groups (Google calls them contact labels) let you organize contacts into named sets. TTMSFNCCloudGooglePeople keeps them in its Groups collection and mirrors the contacts API: asynchronous operations to fetch, create, edit, and delete groups, plus two calls to move contacts in and out of a group. This chapter covers listing and creating groups and managing their membership.

Listing and creating groups

Call GetGroups to load the account's groups; OnGetGroups fires when the list is ready, and the Groups collection is then populated. Create a group by adding an entry to Groups, setting its DisplayName, and calling CreateGroup. Editing (UpdateGroup) and deleting (DeleteGroup) follow the same pattern as contacts, each reporting through its own On... event. This example lists groups, creates one, and assigns a contact to it — combining the groups and contacts collections.

procedure TForm1.LoadGroups;
begin
  FPeople.GetGroups; // result arrives in OnGetGroups
end;

procedure TForm1.PeopleGetGroups(Sender: TObject;
  const AInfo: TTMSFNCCloudGooglePeopleGroups;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
  I: Integer;
begin
  if not ARequestResult.Success then
    Exit;
  ListBox2.Clear;
  for I := 0 to AInfo.Count - 1 do
    ListBox2.Items.Add(AInfo[I].DisplayName);
end;

procedure TForm1.CreateGroup(const ADisplayName: string);
var
  Group: TTMSFNCCloudGooglePeopleGroup;
begin
  Group := FPeople.Groups.Add;
  Group.DisplayName := ADisplayName;
  FPeople.CreateGroup(Group); // result arrives in OnCreateGroup
end;

procedure TForm1.AssignContactToGroup(AGroupIndex, AContactIndex: Integer);
begin
  // Combines the groups and contacts collections to manage membership.
  FPeople.AddContactToGroup(FPeople.Groups[AGroupIndex], FPeople.Contacts[AContactIndex]);
end;

Adding and removing members

Membership is managed with AddContactToGroup and DeleteContactFromGroup, each taking a group and a contact from the respective collections. The change is applied on the server and reported through OnAddContactToGroup or OnDeleteContactFromGroup. A contact's current memberships are available on its own Groups collection, so you can show which labels a contact belongs to after loading it.

Pitfalls

  • Load both collections first. AddContactToGroup and DeleteContactFromGroup take items from Groups and Contacts; call GetGroups and GetContacts (and wait for their events) before managing membership.
  • Use the collection items, not freshly built objects. The membership and update calls rely on each item's server ID, which only exists on entries that came from the server or from a completed CreateGroup/CreateContact.
  • Check ARequestResult.Success in each completion event before assuming the change was applied.

See also