Deep Dive Into SharePoint Framework (SPFx) Webpart Configuration - Part One

SharePoint is one of the most powerful collaboration tools which has been around for decades and is evolving day by day. Another jewel in its crown is SharePoint Framework is both a page and webpart model which provides full support for client-side development.

In this article we are not going to see how to develop it, we are going one step forward and discussing its configuration options available. If you want to learn about building SharePoint Framework webparts and extensions, please follow the below links,

How to change SPFx bundle file name?

Whenever you run gulp bundle –ship command, a new bundle file will be created with some prefix and it appends with GUID at the end. GUID is auto generated to maintain the uniqueness of a file, but you can change the prefix of that bundle file.

Go to config folder in your project and look for config.json, the highlighted content in the below snippet is your bundled file’s prefix. You can change it as per your wish with a meaningful name.

  1. {  
  2.   "$schema": "https://dev.office.com/json-schemas/spfx-build/config.2.0.schema.json",  
  3.   "version": "2.0",  
  4.   "bundles": {  
  5.     "first-sp-fx-web-part": {  
  6.       "components": [  
  7.         {  
  8.           "entrypoint": "./lib/webparts/firstSpFx1/FirstSpFxWebPart.js",  
  9.           "manifest": "./src/webparts/firstSpFx1/FirstSpFxWebPart.manifest.json"  
  10.         }  
  11.       ]  
  12.     }  
  13.   },  
  14.   "externals": {},  
  15.   "localizedResources": {  
  16.     "FirstSpFxWebPartStrings": "lib/webparts/firstSpFx1/loc/{locale}.js"  
  17.   }  
  18. }  

What happens if I change the webpart folder name in SPFx?

If you change the webpart folder name alone without changing the appropriate configurations, you will end up with the error like below, “No localized files found under the root directory”

SharePoint 

So, whenever you change the webpart folder name make sure to change the below localized resources path in config.json

  1. {  
  2.   "$schema": "https://dev.office.com/json-schemas/spfx-build/config.2.0.schema.json",  
  3.   "version": "2.0",  
  4.   "bundles": {  
  5.     "first-sp-fx-web-part": {  
  6.       "components": [  
  7.         {  
  8.           "entrypoint": "./lib/webparts/firstSpFx1/FirstSpFxWebPart.js",  
  9.           "manifest": "./src/webparts/firstSpFx1/FirstSpFxWebPart.manifest.json"  
  10.         }  
  11.       ]  
  12.     }  
  13.   },  
  14.   "externals": {},  
  15.   "localizedResources": {  
  16.     "FirstSpFxWebPartStrings": "lib/webparts/firstSpFx1/loc/{locale}.js"  
  17.   }  
  18. }  

How to change the solution name in SPFx?

How to change SPFx app version?

How to publish SPFx app assests to Office 365 CDN?

How to change the package (.sppkg) file name in SPFx?

The answer for all these questions resides in same file in the location config/package-solution.json

  1. {  
  2.   "$schema": "https://dev.office.com/json-schemas/spfx-build/package-solution.schema.json",  
  3.   "solution": {  
  4.     "name": "spfx-1-client-side-solution",  
  5.     "id": "4b4c2d39-b01c-48c9-894d-22781dda4fb6",  
  6.     "version": "0.0.0.3",  
  7.     "includeClientSideAssets": true,  
  8.     "skipFeatureDeployment": true  
  9.   },  
  10.   "paths": {  
  11.     "zippedPackage": "solution/spfx-1.sppkg"  
  12.   }  
  13. }  

In the above snippet I have highlighted answers for the above questions, where you could change the version/solution name/publish in office 365 CDN/package name.

Please refer to this blog to host webpart in office 365 CDN.

How to hide SPFx webpart from toolbox?

Even after deploying the SPFx webparts globally we have got an option to hide it from normal users to directly add the webpart in the page. To enable this setting, please go to the WebPart.manifest.json file in file location,

/src/webparts/{webpartname}/{WebPartName}WebPart.manifest.json

A property called “hiddenFromToolbox” is used to hide the webparts in toolbox, which will look like below,

  1. {  
  2.   "$schema": "https://dev.office.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",  
  3.   "id": "045784e8-5861-4078-b2eb-a74288f15c3e",  
  4.   "alias": "FirstSpFxWebPart",  
  5.   "componentType": "WebPart",  
  6.   
  7.   // The "*" signifies that the version should be taken from the package.json  
  8.   "version": "*",  
  9.   "manifestVersion": 2,  
  10.   
  11.   // If true, the component can only be installed on sites where Custom Script is allowed.  
  12.   // Components that allow authors to embed arbitrary script code should set this to true.  
  13.   // https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f  
  14.   "requiresCustomScript": false,  
  15.   "hiddenFromToolbox": true,  
  16.     
  17.   "preconfiguredEntries": [{  
  18.     "groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other  
  19.     "group": { "default": "Other" },  
  20.     "title": { "default": "First-SPFx" },  
  21.     "description": { "default": "First-SPFx description" },  
  22.     "officeFabricIconFontName": "Page",  
  23.     "properties": {  
  24.       "description": "First-SPFx Properties"  
  25.     }  
  26.   }]  
  27. }  

How to place all the SPFx webparts to be appeared in same toolbox folder?

How to change the SPFx webpart title?

Answers for these questions reside in the webpart manifest file which is in the below location to do the settings for that

/src/webparts/{webpartname}/{WebPartName}WebPart.manifest.json 

 

  1. "preconfiguredEntries": [{  
  2.     "groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other  
  3.     "group": { "default": "Other" },  
  4.     "title": { "default": "First-SPFx" },  
  5.     "description": { "default": "First-SPFx description" },  
  6.     "officeFabricIconFontName": "Page",  
  7.     "properties": {  
  8.       "description": "First-SPFx Properties"  
  9.     }  
  10.   }]  

If you maintain the same group id and same group name in all your apps, you will be able to see all SPFx webapps under one folder.

Where to mention the external CDN path in SPFx?

If you have decided to host the SPFx app external CDN other than Office 365 public CDN, where to specify the base CDN path in SPFx? There is a setting for this in the config/write-manifests.json file which is highlighted below,

  1. {  
  2.   "$schema": "https://dev.office.com/json-schemas/spfx-build/write-manifests.schema.json",  
  3.   "cdnBasePath": "https://azureCDNUrl/container/Spfx"  
  4. }  

Note: If you specify the external CDN path in the above image then includeClientSideAssets property setting in config/package-solution.json will be ignored and assets won’t be deployed in Office 365 CDN along with .sppkg file. A warning will be displayed in command prompt for the same as below,

SharePoint  

What will happen if I run gulp clean in SPFx?

This is a predefined task in SPFx package which cleans up all the files/folders that are created temporarily at runtime. For example, it will delete the folders like lib, temp, dist,. Don’t worry these folders will be created again when you build/bundle the SPFx apps.

Where to specify the azure storage container details in SPFx?

If you want to host your SPFx apps in Azure CDN storage container, SPFx provides options for that as well in the config/deploy-azure-storage.json file. Use the gulp task gulp deploy-azure-storage

  1. {  
  2.   "$schema": "https://dev.office.com/json-schemas/spfx-build/deploy-azure-storage.schema.json",  
  3.   "workingDir": "./temp/deploy/",  
  4.   "account": "<!-- STORAGE ACCOUNT NAME -->",  
  5.   "container": "spfx-1",  
  6.   "accessKey": "<!-- ACCESS KEY -->"  
  7. }  

  • workingDir - all files from this location will be moved to Azure
  • account - Azure storage account name
  • container - storage container name of this app
  • accessKey - Key to access the storage account

Please follow this blog to know more about CDN.

In this article we have discussed some of the configuration setup and its usage with some screen shots in SPFx. Still there are some more to go and new set of configurations are expected to come in the upcoming SPFx releases which you can find in my next article.

If you have any questions/issues about this article, please let me know in the comments.