Provisioning SharePoint Site Columns Using SPFx Solution

Let us look at provisioning SharePoint site columns (fields) using SharePoint Framework components.

Site Columns can be created in multiple ways. Here, we are looking at adding site columns using definition file (XML) with SPFx solution.

In the following sample, let us try to create different types of fields using SPFx project.

Adding Fields

Create a SharePoint framework project by running the command yo @microsoft/sharepoint or use the existing project.

To add the site column, add the site column definition file to assets folder (asset folder to be created under root SharePoint folder) under the SPFx project. Refer to my previous post for more details.

The below XML shows the definition for creating a custom filed (columns). The definition contains three different fields (text, number and user) to be created. The feature IDs of each and every field is unique, which can be generated using Visual Studio tools. Copy the below piece of xml content.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <Elements xmlns="http://schemas.microsoft.com/sharepoint/">  
  3.     <Field ID="{C73155F8-745A-47F7-893E-FC2C9363E69F}"  
  4.             Name="SPFxTextCol"  
  5.             DisplayName="SPFxTextCol"  
  6.             Type="Text"  
  7.             Required="FALSE"  
  8.             Group="SPFxCustomColumns" />  
  9.   
  10.     <Field ID="{1E65DB1C-DE2B-4A14-8CA6-8BFB6CDBD943}"   
  11.             Name="SPFxNumberCol"  
  12.             DisplayName="SPFxNumberCol"  
  13.             Type="Number"  
  14.             Required="FALSE"  
  15.             Group="SPFxCustomColumns" />  
  16.   
  17.     <Field ID="{B3D8C888-B3D1-4A73-9617-61F746B9457C}"  
  18.             Name="SPFxUserCol"  
  19.             DisplayName="SPFxUserCol"  
  20.             Type="User"  
  21.             Required="FALSE"  
  22.             Group="SPFxCustomColumns" />  
  23.   
  24. </Elements>  

Then, the feature is created for all the fields. The site columns definition file should be mapped to the feature. The package-solution.json file under config folder contains a feature mapping definition. Add the feature title, description, feature id, version and the assets. The assets include the files to be included in the feature.

The following snippet shows the solution JSON along with feature details and package path.
  1. {  
  2.   "solution": {  
  3.     "name": "spassetspfxsolution-client-side-solution",  
  4.     "id": "fc3ce3bf-07c7-4029-bbcc-21e440d37658",  
  5.     "version": "1.0.0.0",  
  6.     "features": [{  
  7.       "title": "spassets-solution",  
  8.       "description": "spassets-solution",  
  9.       "id": "d5499ce4-ba83-40b8-a3ce-c0f4bf9b5595",  
  10.       "version": "1.0.0.0",  
  11.       "assets": {  
  12.         "elementManifests": [  
  13.           "elements.xml"  
  14.         ]  
  15.       }  
  16.     }]  
  17.   },  
  18.   "paths": {  
  19.     "zippedPackage": "solution/spassetspfxsolution.sppkg"  
  20.   }  
  21. }  
Note

The feature id / column ID is a GUID generated using visual studio generate GUID tool.  
 
Package, Deploy & Test

Bundle the solution and upload the bundle files to CDN path. Update the CDN path in the manifest file.

Package and deploy the solution to the SharePoint site (Office 365 site) using app catalog site.

The feature can be activated or deactivated from the features page. The site columns will be present under the columns page (/_layouts/15/mngfield.aspx).

The following article shows the detailed steps for bundling, packaging and deploying.

Thus, you have learned adding/provisioning SharePoint list using SharePoint Framework solutions.