Tarryn
Tarryn

Reputation: 31

Restrict method with Outlook returning nothing

Trying to work out how to create the right filter for the restrict method. I am trying to only show the calendar appointments for a particular user. I have so far got it to show all appointments for a date filter, but ideally need to filter only the organiser. I have tried something like RestrictString := ('[requiredAttendees] = 'Joe Bloggs');, but it doesn't work.

Below is my code to list all appointments and show the appointment organiser (required attendees), as well as subject and start times etc. But I'd like to only do this for a given person.

  Namespace := Outlook.GetNamespace('MAPI'); // represents a NameSpace object.
  Recipient := Namespace.CreateRecipient('[email protected]');
  Recipient.Resolve;
  Folder := Namespace.GetSharedDefaultFolder(Recipient, olFolderCalendar);
  Items := Folder.Items;

  Items.Sort('[Start]', true);
  Items.IncludeRecurrences := true;

   RestrictString :=
   '@SQL="http://schemas.microsoft.com/mapi/proptag/0x0E04001F" like ''%joe bloggs%''';

  RestrItems := Items.Restrict(RestrictString);
  Memo1.clear;
  if (RestrItems.count = 0) then Memo1.lines.add('No appointments');

  for i := 1 to RestrItems.count do
  begin

    Appointment := Items.Item(i);

    Memo1.lines.add(' ');
    Memo1.lines.add(Appointment.requiredAttendees);
    Memo1.lines.add(Appointment.subject);
    Memo1.lines.add(Appointment.Start);
    Memo1.lines.add(Appointment.End);
    Memo1.lines.add(' ');

  end;

  Outlook := UnAssigned;

Any pointers most appreciated. I've looked through various other questions and documentation, but have not quite grasped it yet.

Upvotes: 3

Views: 411

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66225

Restrict/Find methods in Outlook Object Model do not create a subrestriction on recipients (or attachments). You can do that in Extended MAPI (C++ or Delphi), but not in Outlook Object Model.

The best you can do in OOM is to search on the PR_DISPLAY_TO Extended MAPI property (you can see it in OutlookSpy if you click the IMessage button). It corresponds to the To property in OOM and is populated with the ";" separated display names of recipients when the item is saved. It may or may not include the email addresses. The following query should work for Items.Restrict in your case

@SQL="http://schemas.microsoft.com/mapi/proptag/0x0E04001F" like '%Joe Bloggs%'

If using Redemption (I am its author) is an option, it versions of RDOItems.Restrict/Find do create subrestrictions on recipient name / address / SMTP address (you can specify To, CC, BCC, or Recipients in the SQL query).

Upvotes: 2

Related Questions