March 19, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

Building Solutions for Microsoft Teams and SharePoint – PnP Tutorial

The PnP Community is an open Microsoft 365 community of enthusiasts who share the same excitement and love of SharePoint and Microsoft 365.

Aiming at simplifying and empowering the Developers and IT Pros with extensive set of tools.

 

Getting started

If there is one starting point, it has to be the https://aka.ms/m365pnp, which offers loads of resources from such as Webinars, Learning resources, SDK, Yeoman generators, Scaffolding tools …etc.

 

Microsoft 365 PnP Assets

 

Microsoft Graph

Being at the centre of M365, Microsoft graph facilitate the different features that we all take for granted nowadays. When building our modern solutions, it is almost mandatory to be familiar (at least) with Graph API.

Knowing and having some sort of exposure to the previous APIs such as SharePoint Rest Api or SharePoint CSOM will also be very beneficial for building enterprise solutions for M365.

What is Microsoft Graph

First time presented by Satya as :

“A strong data asset that truly enables AI-first workloads” 🤔

Well, it is just a Rest API, which allows you accessing data from :

  • Microsoft 365
  • Azure AD, Intune etc.
  • Windows 10 Services
  • Dynamics 365 Business Central

In some scenarios, you can get the same data from different urls.

How to get started

First place to check, of course the official Documentation !! dah.

The Microsoft Graph API Reference (https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0), which is the complete guide to all the operations

  • What the operation does
  • What permissions are required (for delegate or Application use)
  • How to make the HTTP Request (header, body for Post, Put or Patch requests)
  • and, finally an example of the request/response.

How to use Graph in your App

The Graph explorer, is the main tool you can use to test Graph and the different requests you will be using/building for your solutions.(https://developer.microsoft.com/en-us/graph/graph-explorer)

You can also use PostMan tool (https://www.postman.com/product/rest-client/)

How to use Graph in your application

There are two ways of calling Graph,

  • Construct the HTTP request yourself
  • Use the Graph SDK, which is supposed to build the queries in the back end and has an extensive syntax help. There is a Graph SDK for most of the dev environments.

Troubleshooting

The story of my life 😁

No seriously, there are three possibilities of where the issue can be coming from: your code, the HTTP Request, or the API itself.

It is good to have some tools to troubleshoot. Probably the easiest is to have an HTTP Traffic Monitoring tool (such as Telerik Fiddler).

Different responses code can give you a hint at what might be wrong with your code.

If you can’t identify the issue in your app, try to test it outside your application (Graph Explorer or PostMan)

If no luck, the bug might be from the API Side.

Paging

When querying large entities, Graph will automatically paginate the results.

If we take the example of list, the default page size is 100. However, you can specify the number of items you want returned by the query.

No need to say, you need to consider the performances aspect when thinking about paging.

Thankfully, Graph returns also a link to get the next set of data (next page) @odata.nextLink

ODAta Query string parameters

You can use the $Filter parameter to (guess ??), well, filter the results 😁. However, this has been in beta version for long time now.

$Select can be used to get only the properties you want from the results, instead of getting everything

Other parametres that you can pass in your get query are: $top, $skip, $orderby, $search.

Throttling

HTTP Response 429 ‘Throttled’, which means, you are making too many requests to the Graph Api.

Why ? well, to avoid DOS attacks.

SharePoint Framework

The lovely SPFx ,

You can build for either SharePoint, Viva or Teams, same solutions can be deployed to both environments, so develop once and deploy e

 

  • Web Parts (Personal Apps, Teams Tab, Meeting Apps).
  • Adaptive Card
  • Web parts extensions (to extend Viva Connections, Modern App Pages)
  • Extensions (e.g. Pop Up menu of a doc library)

 

Graph vs SharePoint Rest API

Ideally, we should only use Graph Api (it breaks my heart to hear this 😩).

However, there are some scenarios where SP Rest is still required/betters 🤜🤛 such as:

  • Missing operations
  • Search API
  • SharePoint Framework (you are already authenticated, why would you bother doing an extra authentication for Graph API).
  • Power Automate (make HTTP request to SharePoint, easy and simple operation, but more importantly Free operation)