Calendars
An account can hold several calendars — a default calendar plus any
additional ones the user created, and calendars that other people have
shared with them. TTMSFNCCloudMicrosoftOutlookCalendar lists all of them
together, and lets you create, rename and delete a calendar. This chapter
covers listing calendars (including shared ones) and managing them.
Listing calendars, including shared calendars
GetCalendars retrieves every calendar available to the account into the
Calendars collection, raising OnGetCalendars when the result is
available. Because the component requests the Calendars.Read.Shared
permission automatically (see Authentication), the
result includes calendars other people have shared with the signed-in user
alongside their own — there is no separate flag on TTMSFNCCloudMicrosoftOutlookCal
to tell the two apart; use Summary (the calendar's display name) to
recognize a specific one.
procedure TForm1.ListAllCalendars;
begin
TMSFNCCloudMicrosoftOutlookCalendar1.OnGetCalendars := OutlookCalendarsListed;
// Includes calendars shared with the account: the component requests
// access to shared calendars automatically during authentication.
TMSFNCCloudMicrosoftOutlookCalendar1.GetCalendars;
end;
procedure TForm1.OutlookCalendarsListed(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
i: Integer;
begin
if not ARequestResult.Success then
Exit;
for i := 0 to TMSFNCCloudMicrosoftOutlookCalendar1.Calendars.Count - 1 do
Memo1.Lines.Add(TMSFNCCloudMicrosoftOutlookCalendar1.Calendars[i].Summary);
end;
void __fastcall TForm1::ListAllCalendars()
{
TMSFNCCloudMicrosoftOutlookCalendar1->OnGetCalendars = OutlookCalendarsListed;
// Includes calendars shared with the account: the component requests
// access to shared calendars automatically during authentication.
TMSFNCCloudMicrosoftOutlookCalendar1->GetCalendars();
}
void __fastcall TForm1::OutlookCalendarsListed(TObject *Sender,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (!ARequestResult->Success)
return;
for (int i = 0; i < TMSFNCCloudMicrosoftOutlookCalendar1->Calendars->Count; i++)
Memo1->Lines->Add(TMSFNCCloudMicrosoftOutlookCalendar1->Calendars->Items[i]->Summary);
}
Creating, renaming and deleting a calendar
AddCalendar creates a new calendar with the given name, raising
OnAddCalendar. UpdateCalendar renames an existing calendar, and
DeleteCalendar removes one — both take the TTMSFNCCloudMicrosoftOutlookCal
instance itself (from a prior GetCalendars call), not just its ID.
procedure TForm1.CreateProjectCalendar;
begin
TMSFNCCloudMicrosoftOutlookCalendar1.OnAddCalendar := OutlookCalendarAdded;
TMSFNCCloudMicrosoftOutlookCalendar1.AddCalendar('Project Phoenix');
end;
procedure TForm1.OutlookCalendarAdded(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
// OnAddCalendar reports only success/failure - list again to get the new
// calendar's ID if you need to address it right away.
if ARequestResult.Success then
TMSFNCCloudMicrosoftOutlookCalendar1.GetCalendars;
end;
procedure TForm1.RenameAndDeleteCalendar(ACalendar: TTMSFNCCloudMicrosoftOutlookCal);
begin
TMSFNCCloudMicrosoftOutlookCalendar1.OnUpdateCalendar := OutlookCalendarRenamed;
TMSFNCCloudMicrosoftOutlookCalendar1.OnDeleteCalendar := OutlookCalendarDeleted;
TMSFNCCloudMicrosoftOutlookCalendar1.UpdateCalendar(ACalendar, 'Project Phoenix (archived)');
end;
procedure TForm1.OutlookCalendarRenamed(Sender: TObject;
const ACalendar: TTMSFNCCloudMicrosoftOutlookCal;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
Memo1.Lines.Add('Calendar renamed');
end;
procedure TForm1.OutlookCalendarDeleted(Sender: TObject;
const ACalendar: TTMSFNCCloudMicrosoftOutlookCal;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
if ARequestResult.Success then
Memo1.Lines.Add('Calendar deleted');
end;
void __fastcall TForm1::CreateProjectCalendar()
{
TMSFNCCloudMicrosoftOutlookCalendar1->OnAddCalendar = OutlookCalendarAdded;
TMSFNCCloudMicrosoftOutlookCalendar1->AddCalendar("Project Phoenix");
}
void __fastcall TForm1::OutlookCalendarAdded(TObject *Sender,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
// OnAddCalendar reports only success/failure - list again to get the new
// calendar's ID if you need to address it right away.
if (ARequestResult->Success)
TMSFNCCloudMicrosoftOutlookCalendar1->GetCalendars();
}
void __fastcall TForm1::RenameAndDeleteCalendar(TTMSFNCCloudMicrosoftOutlookCal *ACalendar)
{
TMSFNCCloudMicrosoftOutlookCalendar1->OnUpdateCalendar = OutlookCalendarRenamed;
TMSFNCCloudMicrosoftOutlookCalendar1->OnDeleteCalendar = OutlookCalendarDeleted;
TMSFNCCloudMicrosoftOutlookCalendar1->UpdateCalendar(ACalendar, "Project Phoenix (archived)");
}
void __fastcall TForm1::OutlookCalendarRenamed(TObject *Sender,
TTMSFNCCloudMicrosoftOutlookCal *ACalendar,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (ARequestResult->Success)
Memo1->Lines->Add("Calendar renamed");
}
void __fastcall TForm1::OutlookCalendarDeleted(TObject *Sender,
TTMSFNCCloudMicrosoftOutlookCal *ACalendar,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (ARequestResult->Success)
Memo1->Lines->Add("Calendar deleted");
}
Combining a calendar listing with creating a new one
Checking whether a calendar with a given name already exists before creating it avoids ending up with duplicate calendars of the same name:
procedure TForm1.EnsureProjectCalendarExists;
begin
TMSFNCCloudMicrosoftOutlookCalendar1.OnGetCalendars := ListedThenEnsureCalendar;
TMSFNCCloudMicrosoftOutlookCalendar1.GetCalendars;
end;
procedure TForm1.ListedThenEnsureCalendar(Sender: TObject;
const ARequestResult: TTMSFNCCloudBaseRequestResult);
var
i: Integer;
Found: Boolean;
begin
if not ARequestResult.Success then
Exit;
Found := False;
for i := 0 to TMSFNCCloudMicrosoftOutlookCalendar1.Calendars.Count - 1 do
if TMSFNCCloudMicrosoftOutlookCalendar1.Calendars[i].Summary = 'Project Phoenix' then
Found := True;
if not Found then
TMSFNCCloudMicrosoftOutlookCalendar1.AddCalendar('Project Phoenix');
end;
void __fastcall TForm1::EnsureProjectCalendarExists()
{
TMSFNCCloudMicrosoftOutlookCalendar1->OnGetCalendars = ListedThenEnsureCalendar;
TMSFNCCloudMicrosoftOutlookCalendar1->GetCalendars();
}
void __fastcall TForm1::ListedThenEnsureCalendar(TObject *Sender,
TTMSFNCCloudBaseRequestResult *ARequestResult)
{
if (!ARequestResult->Success)
return;
bool Found = false;
for (int i = 0; i < TMSFNCCloudMicrosoftOutlookCalendar1->Calendars->Count; i++)
if (TMSFNCCloudMicrosoftOutlookCalendar1->Calendars->Items[i]->Summary == "Project Phoenix")
Found = true;
if (!Found)
TMSFNCCloudMicrosoftOutlookCalendar1->AddCalendar("Project Phoenix");
}
Common mistakes
- Assuming a shared calendar is writable. The calendar being listed does not guarantee write access; a request against a calendar the account only has read access to still fails at the service level.
- Reading
CalendarsbeforeOnGetCalendarsfires. The collection is only populated once the request completes. - Deleting the wrong calendar instance.
DeleteCalendarandUpdateCalendaroperate on theTTMSFNCCloudMicrosoftOutlookCalobject itself; pass the exact instance from a currentCalendarslisting, not one reconstructed by hand.
Related API
TTMSFNCCloudMicrosoftOutlookCalendar—GetCalendars,AddCalendar,UpdateCalendar,DeleteCalendar,Calendars,OnGetCalendars,OnAddCalendar,OnUpdateCalendar,OnDeleteCalendarTTMSFNCCloudMicrosoftOutlookCal—ID,Summary