SharePoint Framework offers powerful set of classes that enable communication and data retrieval from various data sources: SharePoint, Microsoft Graph, third-party APIs. This session presents all use cases for connection to APIs from SharePoint Framework, with corresponding code samples and configuration guidance. We will also describe some caveats that can happen during configuration of the permission request, and how to work around them. If you want to learn how to improve your SharePoint Framework solutions to connect to REST APIs, this is the session you won't miss.
3. Dragan Panjkov
Premier Field Engineer, Microsoft
Website and blog: www.dragan-panjkov.com
Twitter: @panjkov
Github: www.github.com/panjkov
4. Consume REST APIs in SPFx
• We almost always work with data
• SPFx provides us with APIs for working with external data sources
• APIs are based on HttpClient
SPHttpClient
MSGraphClient
AADHttpClient
5. HTTPClient
• Provided on the current SharePoint Context
• this.context.httpClient
• Optimized for REST APIs
• Fully configurable HTTP headers & payload
• Can be used for anonymous or secured REST APIs
7. APIs protected with Azure AD
• Declare API permission request in SPFx manifest
• Initialize AadHttpClient in code
• Perform calls using client
• WARNING: API Permission request has to be approved in Tenant
Administration
12. Microsoft Graph
• Single endpoint that proxies multiple Microsoft services
• Allows for easy traversal of objects and relationships
• Simplifies token acquisition and management
• Eliminates the need to traditional discovery (using “me” and
“myorganization”)
13. SharePoint Framework Includes a Microsoft
Graph Client
• MSGraphClient: SharePoint Framework’s Microsoft Graph Client
• Abstracts the token acquisition from the SharePoint Framework’s
support for Azure AD
• Wraps the Microsoft Graph JavaScript SDK and initializes it with one
line that returns a promise
14. Connecting to Microsoft Graph
• Declare API permission request in SPFx manifest
• Initialize MSGraphClient in code
• Perform calls using client
• WARNING: API Permission request has to be approved in Tenant
Administration
15. Using the MSGraphClient
this.context.msGraphClientFactory
.getClient()
.then((client: MSGraphClient): void => {
// use MSGraphClient here
client
.api('/me’)
.get((error: any, user: MicrosoftGraph.User, rawResponse?: any) => {
console.log('name: ', user.displayName);
console.log('email: ', user.mail);
console.log('phone: ', user.businessPhones[0]);
});
});
});
16. Resources
Connect to Anonymous APIs
https://docs.microsoft.com/sharepoint/dev/spfx/connect-to-anonymous-apis
Connect to Azure AD-secured APIs in SharePoint Framework Solutions
https://docs.microsoft.com/sharepoint/dev/spfx/use-aadhttpclient
Use the MSGraphClient to Connect to Microsoft Graph
https://docs.microsoft.com/sharepoint/dev/spfx/use-msgraph
SharePoint Framework web part and K2 Workflow REST API
https://www.dragan-panjkov.com/sharepoint-framework-web-part-and-k2-workflow-rest-api
Lab - Leverage the Microsoft Graph & 3rd Party APIs
https://github.com/SharePoint/sp-dev-training-spfx-graph-3rdpartyapis/blob/master/Lab.md
17. #SPSMUC
SharePoint and Office 365 Saturday Munich
30 November 2019 ⃒ Microsoft Munich
#SPSMUC
Don‘t forget to
rate the session!
THANK YOU!