msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

How to get Email-Address of Attendees (Event)

Open basementmedia2 opened this issue 2 years ago • 2 comments

Sorry again, to maybe ask another stupid question. But how can i get the Email-Address of all attendees of an event?

I want to learn and always try myself first, here is the result:

$datum="2022-03-07";
$datum_start=$datum."T00:00:00";
$datum_end=$datum."T23:59:00";

$termine = $graph->createRequest("GET", "/users/[email protected]/calendarView?startdatetime=".$datum_start."&enddatetime=".$datum_end)
->addHeaders(array(
    'Prefer'        => 'outlook.timezone="W. Europe Standard Time"'
))
->setReturnType(Model\Event::class)
->execute();
   
foreach ($termine as $termin) {
    $attendees = $termin->getAttendees(); // works
    foreach ($attendees as $attendee) {
        print_r($attendee); // works
        $email=$attendee->getEmailAddress()->getAddress(); // works NOT
    }
}

In general, I'm always not sure how to get certain elements of the request-return object. In this case, the object (attendee) of the foreach is structured as follows:

Array ( [type] => required [status] => Array ( [response] => none [time] => 0001-01-01T00:00:00Z ) [emailAddress] => Array ( [name] => Daniel Baeuerlein [address] => [email protected] ) )

Until now, I thought you could access an element by concocted a corresponding function name from the name of an element (e.g. [emailAddress]) (so e.g. getEmailAddress();). But it doesn't seem to work that simple. Is there any logical rule how to access certain elements? Or is it possible to read about it somewhere?

Same e.g. with [type], getType() results in an error. Where can i find information how to get the type?

Thank you again for help

Best wishes

Daniel

Thanks for your help

basementmedia2 avatar Mar 07 '22 10:03 basementmedia2

Really nobody that can help ;-( ?

basementmedia2 avatar Mar 17 '22 06:03 basementmedia2

Sorry for the delay @basementmedia2

Your thinking around the correct function to call based on an attribute e.g. [type] => getType, is correct

There is a bug with the current v1 of the SDK where nested collections don't get deserialized into models e.g. your request returns a response which is deserialized into an array of Events but within each event, getAttendees() returns an array of arrays instead of an array of Attendee objects (I hope this makes sense)

Unfortunately, fixing this would mean a breaking change i.e. changing getters like getAttendees() to return Attendee[] instead of array. The team is currently working on new major release (V2) which will address this issue.

In the meantime, if you're not working on a production application, you can consider using the latest 2.0 Release Candidate.. some guidance on how to add it to your composer.json.

Otherwise, you'd need to instantiate an Attendee object before using the getters i.e.

foreach ($attendees as $attendee) {
        print_r($attendee); 
        $attendee = new Attendee($attendee);
        $email=$attendee->getEmailAddress()->getAddress(); 
    }

Or treat $attendee as an array and access its properties as keys (more difficult, wouldn't advise)


foreach ($attendees as $attendee) {
        print_r($attendee); 
        $email=$attendee['emailAddress']['address'];
    }

Ndiritu avatar Mar 17 '22 08:03 Ndiritu

Nested deserialization of models handled in v2 preview

Ndiritu avatar Oct 06 '22 08:10 Ndiritu