M365 – SPFX – Getting JSON data – JSON serialized information of SPFX component to provision / add on modern client side page through CSOM

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂 I hope we all are safe 🙂 STAY SAFE, STAY HEALTHY 🙂

Background / Details / Use Case:

We have requirement – we need to provision some client side pages in our communication site when its created. So we have a job which doing the magic for us.

One of the requirement is we need to provision the OOB as well as our custom SPFX components to respective client side page.

So to provision / add SPFX components from client side,

  • Load our ClientSidePage using ClientSidePage.Load()
  • We need to instantiate the ClientSideWebPart class
  • We need to set the PropertiesJson property of ClientSideWebPart class
  • Finally add the component using ClientSidePage.AddControl()

Example : Consider we need to add/provision OOB QucikLinks webpart on our one of the client side page and we need to provision default link for “Google”.

Following are the detailed steps:

  • Step 1 – Load our client side page
ClientSidePage clientSidePage =  ClientSidePage.Load(contentContext, pageTitle);
  • Step 2 -Instantiate our OOB QuickLinks SPFX component
 //Quick Links wp
ClientSideWebPart qlWebPart = clientSidePage.InstantiateDefaultWebPart(DefaultClientSideWebParts.QuickLinks);
  • Step 3 – Set the JSON properties – provision the default link – “Google” as
qlWebPart.PropertiesJson = "{\"controlType\":3,\"id\":\"ca330715-e5de-4c9d-9a17-112f345d17c4\",\"webPartId\":\"c70391ea-0b10-4ee9-b2b4-006d3fcad0cd\",\"webPartData\":{\"id\":\"c70391ea-0b10-4ee9-b2b4-006d3fcad0cd\",\"instanceId\":\"ca330715-e5de-4c9d-9a17-112f345d17c4\",\"title\":\"Quick links\",\"serverProcessedContent\":{\"searchablePlainTexts\":{\"items[0].title\":\"Google\"},\"links\":{\"items[0].sourceItem.url\":\"http://google.co.in\"}}]}";
  • Step 4 – Add component on the page
clientSidePage.AddControl(qlWebPart, sectionFirst.Columns[1], 0);

Complete code:

ClientSidePage clientSidePage =  ClientSidePage.Load(contentContext, pageTitle);

//Quick Links wp
ClientSideWebPart qlWebPart = clientSidePage.InstantiateDefaultWebPart(DefaultClientSideWebParts.QuickLinks);

qlWebPart.PropertiesJson = "{\"controlType\":3,\"id\":\"ca330715-e5de-4c9d-9a17-112f345d17c4\",\"webPartId\":\"c70391ea-0b10-4ee9-b2b4-006d3fcad0cd\",\"webPartData\":{\"id\":\"c70391ea-0b10-4ee9-b2b4-006d3fcad0cd\",\"instanceId\":\"ca330715-e5de-4c9d-9a17-112f345d17c4\",\"title\":\"Quick links\",\"serverProcessedContent\":{\"searchablePlainTexts\":{\"items[0].title\":\"Google\"},\"links\":{\"items[0].sourceItem.url\":\"http://google.co.in\"}}]}";

clientSidePage.AddControl(qlWebPart, sectionFirst.Columns[1], 0);

This code perfectly works, when application/job executes, OOB Quick links SPFX component get added / provisioned successfully on given client side page.

But pain point is for Step 4 in above steps. To set the JSON string for PopertiesJson property of ClientSideWeb part class

In code if you hover mouse on PropertoesJson property we will get an idea how to get – JSON serialized web part info – JSON string

M365 - SPFX - details for :PropertiesJson" property of ClientSideWebPart - JSON serialized web part information - JSON string
Fig : M365 – SPFX – details for :PropertiesJson” property of ClientSideWebPart – JSON serialized web part information – JSON string

How to get the JSON value for PropertiesJson property:

M365 - SPFX - workbench.aspx application page
Fig : M365 – SPFX – workbench.aspx application page
  • Add the OOB QuickLinks SPFX component on workbench.aspx page as
 M365 - SPFX - workbench.aspx application page - Adding OOB QuickLinks SPFX component
Fig : M365 – SPFX – workbench.aspx application page – Adding OOB QuickLinks SPFX component
  • Add the link – Google and click on “save” in left top corner as
M365 - SPFX - workbench.aspx application page - Adding OOB QuickLinks SPFX component - Adding link

Fig : M365 – SPFX – workbench.aspx application page – Adding OOB QuickLinks SPFX component – Adding link

  • Once ready, there is a link called “Web part data” – which will open popup and will give us the JSON data which can be set to PropertiesJson property of ClientSideWebPart class as
M365 - SPFX - workbench.aspx application page -Getting "JSON serialized web part information - JSON string" for OOB QuickLinks SPFX component - for modern pages
Fig : M365 – SPFX – workbench.aspx application page -Getting “JSON serialized web part information – JSON string” for OOB QuickLinks SPFX component – for modern pages
M365 - SPFX - workbench.aspx application page -Getting "web part information - web part XML" for OOB QuickLinks SPFX component - for classic pages
Fig : M365 – SPFX – workbench.aspx application page -Getting “web part information – web part XML” for OOB QuickLinks SPFX component – for classic pages

There are some extra details as well in current JSON string like – SiteID, WebID, Current site URL – remove those properties or update to set dynamically. Otherwise component will break when we will execute the job for provisioning on other site or other tenant.

I’ll stop here now 🙂

Share In Teams:

Enjoy the beautiful life 🙂 Have a FUN 🙂 HAVE A SAFE LIFE 🙂 TAKE CARE 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: