Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access a different calendar than the main one #435

Open
mblonski2015 opened this issue Apr 13, 2017 · 7 comments
Open

Access a different calendar than the main one #435

mblonski2015 opened this issue Apr 13, 2017 · 7 comments

Comments

@mblonski2015
Copy link

Hello,
I'd like to read all events from a exchange user's calendar. I can read the main one, but I don't know where I can submit the name of the other folder/calendar.

this is the part I am using:
$client = new Client($host, $username, $password, $version);
$client->setTimezone($timezone);

$request = new FindItemType();
$request->ParentFolderIds = new NonEmptyArrayOfBaseFolderIdsType();

// Return all event properties.
$request->ItemShape = new ItemResponseShapeType();
$request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;

$folder_id = new DistinguishedFolderIdType();
$folder_id->Id = DistinguishedFolderIdNameType::CALENDAR;
$request->ParentFolderIds->DistinguishedFolderId[] = $folder_id;

$request->CalendarView = new CalendarViewType();
$request->CalendarView->StartDate = $start_date->format('c');
$request->CalendarView->EndDate = $end_date->format('c');

Can anybody give me a hint? The other calendars are called "birthday", "Event". Can I submit the real name or do I have to get an ID?

Thanks for your help.

Mathias
a beginner

@jamesiarmes
Copy link
Owner

jamesiarmes commented Jun 5, 2017

Rather than using a distinguished folder id, you need to pass the id of the calendar folder you wish to retrieve the events for. If you don't know that id, you can look it up using the FindFolder operation. It will be a sub-folder of the DistinguishedFolderIdNameType::CALENDAR folder.

Also, in the future, please complete the issue template in full and properly format your code using GFM.

@mblonski2015
Copy link
Author

mblonski2015 commented Jun 8, 2017 via email

@mblonski2015
Copy link
Author

mblonski2015 commented Jun 8, 2017 via email

@bren1818
Copy link

@mblonski2015 - Not sure if you found your answer. What you need is a user with privileges or impersonation rights to the "room" or mailbox you want to look at. Using the newest build, you could try the following:

`<?php
require_once '../../../../../vendor/autoload.php';

use \jamesiarmes\PhpEws\Client;
use \jamesiarmes\PhpEws\Request\FindItemType;

use \jamesiarmes\PhpEws\ArrayType\NonEmptyArrayOfBaseFolderIdsType;

use \jamesiarmes\PhpEws\Enumeration\DefaultShapeNamesType;
use \jamesiarmes\PhpEws\Enumeration\DistinguishedFolderIdNameType;
use \jamesiarmes\PhpEws\Enumeration\ResponseClassType;

use \jamesiarmes\PhpEws\Type\CalendarViewType;
use \jamesiarmes\PhpEws\Type\DistinguishedFolderIdType;
use \jamesiarmes\PhpEws\Type\ItemResponseShapeType;
use \jamesiarmes\PhpEws\Type\EmailAddressType;
use \jamesiarmes\PhpEws\Enumeration\ItemQueryTraversalType;

// Replace with the date range you want to search in. As is, this will find all
// events within the current calendar year.
$start_date = new DateTime('September 25 00:00:00');
$end_date = new DateTime('September 30 23:59:59');
$timezone = 'Eastern Standard Time';

// Set connection information.
$host = 'outlook.youraddress.ca';
$username = 'bren1818'; //your account with permissions
$password = 'MyAwesomePassword'; // your password
$version = Client::VERSION_2013_SP1; //version of exchange

$client = new Client($host, $username, $password, $version);
$client->setTimezone($timezone);

$request = new FindItemType();
$request->Traversal = ItemQueryTraversalType::SHALLOW;
$request->ItemShape = new ItemResponseShapeType();
$request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;

$request->CalendarView = new CalendarViewType();
$request->CalendarView->StartDate = $start_date->format('c');
$request->CalendarView->EndDate = $end_date->format('c');
$folder_id = new DistinguishedFolderIdType();
$folder_id->Id = DistinguishedFolderIdNameType::CALENDAR;
$folder_id->Mailbox = new EmailAddressType();
$folder_id->Mailbox->EmailAddress = "sharedroomtest@wlu.ca";
$request->ParentFolderIds->DistinguishedFolderId[] = $folder_id;
$response = $client->FindItem($request);
$response_messages = $response->ResponseMessages->FindItemResponseMessage;
//var_dump($response_messages);

//if( $response_messages ){
foreach ($response_messages as $response_message) {
// Make sure the request succeeded.
if ($response_message->ResponseClass != ResponseClassType::SUCCESS) {
$code = $response_message->ResponseCode;
$message = $response_message->MessageText;
//fwrite(
// STDERR,
echo "Failed to search for events with "$code: $message"\n";
//);
continue;
}

// Iterate over the events that were found, printing some data for each.
$items = $response_message->RootFolder->Items->CalendarItem;
foreach ($items as $item) {
    $id = $item->ItemId->Id;
    $start = new DateTime($item->Start);
    $end = new DateTime($item->End);
    $output = 'Found event ' . $item->ItemId->Id . "<br />"
        . '  Change Key: ' . $item->ItemId->ChangeKey . "<br />"
        . '  Title: ' . $item->Subject . "<br />"
        . '  Start: ' . $start->format('l, F jS, Y g:ia') . "<br />"
        . '  End:   ' . $end->format('l, F jS, Y g:ia') . "<br /><hr />";

    echo $output . '<br />';
}

}
//}`

@jamesiarmes
Copy link
Owner

As I mentioned in my previous comment, please format all code using GFM.

In your event find request, you need to replace the $request->ParentFolderIds->DistinguishedFolderId with $request->ParentFolderIds->FolderId[], using the id for the appropriate calendar.

@bren1818
Copy link

bren1818 commented Oct 4, 2017

@jamesiarmes Thank you for the response, had to re-read what you wrote to see the difference between what you had said and I had written. That being said, when I tried using $request->ParentFolderIds->FolderId[] I do not get any event data back, while when I use $request->ParentFolderIds->DistinguishedFolderId[] I do get data back...

@mblonski2015
Copy link
Author

mblonski2015 commented Oct 4, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants