Extending the Office 365 Connector module

02 August 2022
2 minutes

Using the Office 365 Connector module it is possible for developers to connect to the Graph API's from Microsoft.

How to start

First of all we need to know what we want to get from, or send to, Microsoft. For this blog I would like to show you how to search for files in SharePoint, but there are a lot more API's available.

Microsoft offers good documentation and a Graph Explorer. Using the Graph Explorer you can test the endpoints and see what data is returned when you do a request.

Custom scopes

When developing it can happen that the scopes the default Office connector module or one of its submodules use isn’t sufficient.

When that happens you can use a custom hook we have added to the module. This hook adds the authorization scopes when logging in to Office and makes the data in the Graph API accessible for the Office 365 connector.

Get data from the Graph API

To retrieve data from the Graph API we have created a service for you called the GraphService. When using this service you have all the benefits of the Office 365 connector. This service already takes in account your status, and retrieves data from the Graph API as the current logged in user.

As you can see in the screenshot it is really easy to use. The getGraphData method in the service returns the data retrieved from the Graph API as is so you can do your own magic with it.

The first parameter for the method is the endpoint in the Graph API which you can find in the Graph Explorer at Microsoft.com.

Post data to the Graph API

To post data to the Graph API you can use the sendGraphData method in the service. Just add the endpoint you would like to post to, and the data structure and you’re done! In the code example you can see how an event is created in my Outlook Calendar.

And now?

Do you want to know more? Take a look at the project page on Drupal.org. And for any questions, please add an issue to the issue queue or ask support questions on our slack channel.

Photo by Ryan Quintal on Unsplash

More articles