Automating deployment of SharePoint Framework Apps


Automating deployment of SharePoint Framework Apps

Please note: that this approach uses the Advance HTTP Operation pattern, which is not normally recommended by Microsoft – it’s worth reading the disclaimer in Microsoft’s description of this approach to understand why. However, at the time of writing this article there was no API or recommended approach to publish SPFx Apps, so this is currently the only practical way to provision SPFx apps in bulk. This is how to efficiently deploy apps to the SharePoint Framework.

You can find source code here.

 

Since the App model was introduced in 2013, a major limitation has been the need to manually enable each App for every subsite. It’s manual, tiresome and overall convoluted – and doesn’t fit in with our DevOps / enterprise deployment best-practices.

One of my recent tasks as an Architect was to proof the delivery of multiple client web parts to 85(!) subsites across 6 site collections. Doing the math, that’s 510 sites!

To enable these web parts, I needed to add the SharePoint Framework App to each subsite. Currently the only way to add the SPFx Apps is manual, by using the Add App SharePoint framework functionality.

 

Whilst this works very well, it only works best when you’re doing this for a couple of sites. For me, this wasn’t the case – with only a few days to complete this work on 510 sites, time was of the essence and I needed a more efficient solution!

Unfortunately, I was unable to find any API or information to automate SPFx Apps, so I decided to start figuring this out by myself. Maybe there will be some improvements announced in March on the PnP webcast (at least for app / SPFx upgrades), but nothing is guaranteed.

To start, I decided to use the storefront.aspx page and simulate a human being clicking at the add it button.

Next, I used Fiddler to discover the requests a page makes during the initial load and after clicking the Add It button.

Initial load – 2 requests:

GET: [URL to your site]/_layouts/15/storefront.aspx?source=[URL to your site - encoded] #vw=AppDetailsView,app=[AppId],clg=1,bm=,cm=

GET: [URL to your site]/_layouts/15/storefront.aspx?task=GetAppDetails&bm=&cm=&appid=[AppId or ProductId]&catalog=1&uirequest=1 – returns JSON and combines it with previous request

After clicking the button – 2 requests:

GET: [URL to your site]/_layouts/15/storefront.aspx?task=CreateAppPrincipal&catalog=1&appcatalogid=[AppId]

POST: [URL to your site]/_layouts/15/storefront.aspx?source=[URL to your site - encoded] #vw=AppDetailsView,app=[AppId],clg=1,bm=,cm=

Based on above discovery I designed the following steps:

  1. Authenticate into my SP Online
  2. Get AppId by using ProductId (ProductId can be easily found in App Manifest or in App Catalog)
  3. Send request to App Page
  4. Extract RequestDigest from the initial request
  5. Get JSON containing App Details with X-RequestDigest Header
  6. Get App Principal ID with X-RequestDigest Header
  7. Build a post body consisted of many values of RequestDigest, ViewState, etc
  8. Post to App Page
  9. Authenticate into my SP Online
  10. Get AppId by using ProductId
  11. Send request to App Page
  12. Extract RequestDigest and other necessary data such as ViewState from the initial request
  13. Get JSON containing App Details with X-RequestDigest Header
  14. Get App Principal ID with X-RequestDigest Header
  15. Build a post body consisted of many values of RequestDigest, ViewState, etc
  16. Post App Page

Okay, that’s all the code, now let’s see it in action!

How beautiful is that, wouldn’t you agree? I was able to add 510 apps in less than ten minutes!

For the SharePoint Framework the solution is using a clean Web Request (not dodgy things like WebBrowser Control). This can therefore be used with any type of Apps – remote timer jobs, provider-hosted add-ins, etc. I applied the same pattern to Provider-hosted Add-Ins and it works like a charm – I’ll share this in a future blog post! Thanks for reading how to automate the deployment of SharePoint Framework Apps!

Leave a comment

Your email address will not be published. Required fields are marked *