0
votes

I'm building a simple contact book from office 365, that lists all the shared contacts of my company. I was trying with Graph and EWS too, but i can't figure out what is wrong.

Searching in the Microsoft Graph explorer seems to be no chance to see my "Other Contacts" -> "All Contacts" folder. I've been trying with "/me/contactFolders" endpoint and with "/people" endpoint. Non of them gave me results.

I also used a php-ews library (the project is build on Laravel) to access to the folders via Exchange, with no luck. using this example, i'm able to list just my contact, without any chance to see other folders or other kind of contacts.

Does anybody have any tip for a newbbie?!

thanks in advance. EDIT this is the Controller that works with PHP-EWS library

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use garethp\ews\ContactsAPI as ContactsAPI;

use garethp\ews\API;
use garethp\ews\API\Enumeration;
use garethp\ews\API\Type;
use garethp\ews\API\ExchangeWebServices;
//use garethp\ews\API\ExchangeAutodiscover;
//use garethp\ews\API\Exception\AutodiscoverFailed;
class SharedContatctsController extends Controller
{
    //
    public function index()
    {
      # code...

      $mail='[email protected]';
      $pwd='password';
      $version='Exchange2016';
      //$apiS = ExchangeAutodiscover::getAPI($mail, $pwd);
      //$server=$apiS->getClient()->getServer();
      $server='mail.example.com;
      $api = ContactsAPI::withUsernameAndPassword($server, $mail, $pwd);
      $contacts = $api->getContacts();
      //return print($api->getFolderId());
      //If you want to get contacts within a folder
      //$folder = $api->getFolderByDisplayName('Altri Contatti', 'contacts');
      //$contacts = $api->getContacts($folder->getFolderId());
      return view('shared',array('contacts'=>$contacts,'displayName'=>$contacts['displayName']));
    }

}

This is the Controller that (quite works) displays the "BackupContacts" folder that is in the same directory as "Contact"

<?php

namespace App\Http\Controllers;
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
use App\Http\Controllers\Controller;

class OutlookController extends Controller
{
public function contacts()
{
  if (session_status() == PHP_SESSION_NONE) {
    session_start();
  }

  $tokenCache = new \App\TokenStore\TokenCache;

  $graph = new Graph();
  $graph->setAccessToken($tokenCache->getAccessToken());

  $user = $graph->createRequest('GET', '/me')
                ->setReturnType(Model\User::class)
                ->execute();

  $contactsQueryParams = array (
    // // Only return givenName, surname, and emailAddresses fields
    //"\$select" => "displayName,scoredEmailAddresses",
    // Sort by given name
    //"\$orderby" => "givenName ASC",
    // Return at most 10 results
    "\$orderby"=>"displayName",
    "\$top" => "1000"
  );

  $getContactsUrl = '/me/contactFolders/{BackuPlderId-retrieved-with-Graph}/contacts/?'.http_build_query($contactsQueryParams);
  $contacts = $graph->createRequest('GET', $getContactsUrl)
                    ->addHeaders(array ('X-AnchorMailbox' => $user->getMail()))
                    ->setReturnType(Model\Contact::class)
                    ->execute();

  return view('contacts', array(
    'username' => $user->getdisplayName(),
    'usermail' => $user->getMail(),
    'contacts' => $contacts
  ));
}
}
1
We are always glad to help and support new coders but you need to help yourself first. :-) After doing more research if you have a problem post what you've tried with a clear explanation of what isn't working and provide a Minimal, Complete, and Verifiable example. Read How to Ask a good question. Be sure to take the tour and read this.Jay Blanchard
Sorry @JayBlanchard. Just Edited to make it Clearer.Biro

1 Answers

2
votes

The /me/contacts will return only the contacts in the current user's default contact folder. Similarly, the /me/contactFolders will only return the contact folders for the current user's mailbox (and an empty result if there isn't anything beyond their default folder.

The key here is the /me element. This is synonymous with currently authenticated user. So if the user who authenticated was [email protected] both /me/contacts and /users/[email protected]/contacts would return the exact same results.

The currently isn't a method for retrieving Organization Contacts (i.e. contacts stored in your GAL) from the /v1.0 API set. There is however an orgContact object in the /beta API so support is forthcoming.