Call Azure AD secured API from your SPFx code series:
- Call Azure AD secured API from your SPFx code. Story #1: Azure Functions with cookie authentication (xhr "with credentials")
- Call Azure AD secured API from your SPFx code. Story #1.1: Azure Web App with ASP.NET Core 2.x and cookie authentication (xhr "with credentials")
- Call Azure AD secured API from your SPFx code. Story #2: Web app (or Azure Function) and SPFx with adal.js <—you are here
- Call Azure AD secured API from your SPFx code. Story #3: Web app (or Azure Function) and SPFx with AadHttpClient
It’s possible to call your remote Azure AD secured API with help of popular adal.js library. This approach has a number of issues (read in the end of the post). Almost all issues come from a fact, that adal.js works well in case of SPA and doesn’t play nicely in SPFx world. To make it work with SPFx, you should “patch” it. Even in this case there are some caveats. That’s why for now recommended approach is using AadHttpClient, however for the sake of completeness I wrote a post on adal.js as well. By the way, AadHttpClient is still in preview (as of now, check the actual state at docs.microsoft.com).
Read more on this topic here – Connect to API secured with Azure Active Directory and here – Call the Microsoft Graph API using OAuth from your web part.
In today’s post we need to perform below steps:
- Add new app registration in Azure AD
- Create Azure AD secured API (Web App with custom jwt bearer authentication or Azure Function with EasyAuth aka App Service Authentication, I will cover both) and enable CORS
- Patch adal.js library to work with SPFx
- Create SPFx web part, which uses adal.js and calls remote Azure AD protected API
The source code for this article available on GitHub here.
Let’s get started More...
SPFx has a built-in mechanism, which holds some configuration values for you. For example in runtime, you can read them and determine if you are running locally or not. You can verify if you are in debug mode or not. You can do some other things as well. Let’s take a closer look at some values, available out-of-the box.
Probably you know, that SPFx build pipeline is webpack based. It means, that webpack’s configuration supplies a lot of those values. The most useful are:
process.env.NODE_ENV - can be either ‘production’ or ‘dev’ depending on an environment where the code is running
DEBUG – boolean, equals to false for release builds and true for development
DATACENTER – boolean, equals to true if you are running in context of SharePoint Online. Might be useful if you build webparts for on-prem and Online at the same time More...
A few days ago I published SharePoint REST API Metadata Explorer. In this post I want to share a bit more information about it and share future plans as well.
SharePoint Rest API for long time was a hidden gem for me. I used CSOM or JSOM without issues and that was fine. Nowadays the power of SharePoint REST API increases. Today REST is a recommended way to interact with SharePoint. BTW for a browser there is a great library to manipulate SharePoint REST API using convenient fluent syntax – PnPjs. If you haven’t tried it yet, it’s time to try .
What is always difficult for me with REST API – to find good documentation for different REST API url along with good usage examples. Some information available at docs.microsoft.com, some in blogs and sharepoint.stackexchange site. However there are few issues with mentioned sources:
- they don’t fully cover REST API, there are still a lot of methods which are not yet covered
- in SharePoint Online every month REST API changes and new methods or method params appear
A few years ago I discovered that SharePoint REST API provides WCF $metadata endpoint. More...
Actually for now these libraries are identical and have feature parity. What is the purpose of having two identical libraries? Because PnPjs is an evolution of original PnP-JS-Core. Consider below points regarding new library:
- new organization name (github/pnp) corresponds to npm modules names (@pnp/)
- new library has better structure organization – there are multiple logically divided modules, thus making support and extensibility easier
- new library has exactly the same API and features as current PnP-JS-Core
- new library is a part of SharePoint Pattern & Practices (SharePoint PnP)
- the same people are behind it – Microsoft and awesome community
- development will be performed simultaneously in two libraries for about 5 months. If one feature is available in PnP-JS-Core, this feature also will be added to PnPjs (and vice versa)
- after 5 months (approximately summer 2018) PnP-JS-Core will be deprecated, it will be available in npm and cdn, however it will never receive new features, because main development will be transitioned to PnPjs (pnp/pnp)
What does it mean for you? If you are starting a new project, use new PnPjs library. If you have a chance to perform an upgrade for your current project, upgrade it to PnPjs. If you have a project with PnP-JS-Core – its fine, because the library will be in npm and on cdn, however new feature won’t be rolled out starting from summer 2018 for PnP-JS-Core specifically.
To listen about upcoming changes checkout PnPjs community call – starting from 19:00. If you want to listen to community calls live – use this calendar invite.
Hope this helps!
Last time I blogged about a way to run PnP-PowerShell from VSTS build. This time I’m going to demo on how to run various gulp tasks interacting with SharePoint (upload file for example). Of course, you can easily do that with PnP-PowerShell, however what if you utilize gulp heavily and want to keep everything in one place. Or file upload is a part of your other gulp-based process. Anyway, there might be cases when you want to do that, and here is how.
Start with a new build definition “empty process”. Refer to previous blog to find out how. For our process we need only two simple steps: More...