Digging into SharePoint Modern Page Text Webpart

Injecting HTML content with PnP PowerShell

NaS IT
4 min readSep 18, 2018

Currently in SharePoint modern page you are able to add text simply in any section with the Text Webpart. This web part offers some styling options, however they don’t offer the option to show and edit the HTML inside the web part in the UI.

To get and edit / add HTML content of the Text Webpart we will have to use the SharePoint PnP PowerShell module.

Text styling and formatting options

Add the Text Webpart to a page section

When you add a Text web part to your page, you can format text with the tools available to you. The basic options are available in a small inline tool bar, but you can access to more options clicking the ellipsis.

Inline text tool bar:

Inline text tool bar

Side text tool bar:

Side text tool bar

When you insert a table and you are editing inside the table, more options for formatting and styling the table will show up.

Table controls:

Table controls and styling options

Below is an example of almost all the sort of styling and formatting you can do within the modern Text Webpart.

The different styling options

Getting the HTML with PnP PowerShell

From the example above we can retrieve

Get-PnPClientSideComponent -Page Home.aspx

This will return all the components (web parts) on the page.

Retrieve the details of the Text Webpart (replace with the instanceId returned in your case).

$textWebpart = Get-PnPClientSideComponent -Page Home.aspx -InstanceId <InstanceId>

Use the following command to copy the HTML content to your clipboard, that you can then past in your favorite code editor.

$textWebpart.Text | clip

HTML content:

Most of the content is just standard HTML (with the usual quirks of these visual editors), but it’s good to notice that the table must be wrapped into 2 div.

<div class="canvasRteResponsiveTable">
<div class="tableWrapper">
<table class="bandedRowTableStyleTheme" title="Table">
<tbody>
...
</tbody>
</table>
</div>
</div>

If you insert a table without these div elements, your table will be visible but wiped if you edit the page from the UI.

You can also note that Header 1 is in fact a <h2> and Header 2 a <h3>

It’s probably better to stick to what the visual editor is producing, to avoid having your content disappearing.

Injecting custom HTML in the Text Webpart

It is really easy to inject your own custom HTML into a Text web part of any of your SharePoint modern pages. Again we will use an available cmdlet of PnP PowerShell.

First put your HTML content into a variable or read it from a file.

$htmlToInject = '
<div class="canvasRteResponsiveTable">
<div class="tableWrapper">
<table class="bandedRowTableStyleTheme" title="Table">
<tbody>
<tr>
<td><strong>D</strong></td>
<td><strong>E</strong></td>
<td><strong>F</strong></td>
</tr>
<tr>
<td>1</td>
<td>10</td>
<td>OK</td>
</tr>
<tr>
<td>2</td>
<td>20</td>
<td>OK</td>
</tr>
<tr>
<td>3</td>
<td>30</td>
<td>OK</td>
</tr>
</tbody>
</table>
</div>
</div>
'

Then add a new Text Webpart with this content.

Add-PnPClientSideText -Page home.aspx -Text $htmlToInject -Section 1 -Column 1 -Order 1

Or edit the content of an existing Text Webpart.

Set-PnPClientSideText -Page home.aspx -InstanceId <InstanceId> -Text $htmlToInject

Hopefully this quick article could be useful to you as a reference to help you programmatically edit and create modern page in SharePoint Online. Beside using this within your Site provisioning process, we could imagine things like creating automatically page with reports, or migrating existing HTML content in SharePoint Site.

Also I didn’t try to inject HTML with the <script> tag but you can try ;).

--

--