How to Update Enhanced Rich Text Field using PowerShell CSOM

Introduction

Have you ever faced a scenario, where updating the Rich Text field and Enhanced Rich Text field based on the content from a html file? I was working on solution recently where the ‘Multiline text’ with Enhanced Rich Text needs to be updated from the PowerShell CSOM. I have followed these steps to make it work.

First thing I want to provide my heartful thanks to my thanks to Abby Martins who helped me in getting this wonderful script working and want to give credit to Salaudeen Rajack from https://sharepoint-dairy.com.

Here I am using ListItemCreationInfo class which is coming from Microsoft.SharePoint.Client module which is from SharePoint 2016 Client Side Object Model. More about this ListItemCreationInfo class can be found from the references section.

Pre-Requisites

  • Ability for the SharePoint site to have minimum contribute rights.
  • SP2016 CSOM PowerShell Module installed on your machine.

Steps

Step 1. Load the SharePoint 2016 CSOM Assemblies.

# Load the assemblies for the SharePoint 2016 CSOM
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")  
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") 

Step 2. Set the Site Url and List Name values.

#Set Site URL and the list name parameters
$SiteURL="http://fin.accessgmf.com/cf"
$ListName="Notifications"

Step 3. Store the credentials in the PowerShell variable.

#Get Credentials to connect to SharePoint site
$Creds = Get-Credential

Step 4. Set up the PowerShell context to run against the site.

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object System.Net.NetworkCredential($Creds.UserName, $Creds.Password)

Step 5. Set the List item creation information, item and title using the CSOM client libraries.

#Get the List
$List=$Ctx.Web.Lists.GetByTitle($ListName)
#Set the list item creation information using CSOM libraries
$ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
$ListItem = $List.AddItem($ListItemInfo)

Step 6. Get the content from html files and store in a PS(PowerShell) variable.

#Set Column Values
$ListItem["Title"] = "TestHtmlMessage" 
#Get the Html message from the location and pass the html message as string
$messageDescription =  Get-Content "C:\Users\vayina1\Documents\Sample\SampleHtml.html"   
$ListItem["Description"] = "$($messageDescription)"

Step 7. Create the item in SharePoint list.

#add item to sharepoint online list powershell
$ListItem.Update()
$Ctx.ExecuteQuery()

Step 8. Validate the output by logging into site and validating the list item. 

Update Enhanced Rich Text Field using PowerShell CSOM

Note

Kindly note that these steps are run in SharePoint 2016 on-premises environment. Same script should work against SharePoint online, but it is required to add SharePoint online CSOM libraries and authenticate it using MSAL. You can refer to connecting to SharePoint online using CSOM in references section.

Complete Script

# Load the assemblies for the SharePoint 2016 CSOM
$loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")  
$loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")  

#Set Site URL and the list name parameters
$SiteURL="https://contosointernal/sites/TestSite" #Replace it with your on-premise SharePoint URL
$ListName="Notifications" #Replace it with list name specific to your requirement
 
Try {
    #Get Credentials to connect to SharePoint site
    $Creds = Get-Credential
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object System.Net.NetworkCredential($Creds.UserName, $Creds.Password)
  
    #Get the List
    $List=$Ctx.Web.Lists.GetByTitle($ListName)
  
    #Set the list item creation information using CSOM libraries
    $ListItemInfo = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation 
    $ListItem = $List.AddItem($ListItemInfo)
     
    #Set Column Values
    $ListItem["Title"] = "TestHtmlMessage"
 
    #Get the Html message from the location and pass the html message as string
    $messageDescription =  Get-Content "C:\Users\vayina1\Documents\Sample\SampleHtml.html"  
    #Pass the html content in a string format. This is the trick :) 
    $ListItem["Description"] = "$($messageDescription)"
     
 
    #add item to sharepoint online list powershell
    $ListItem.Update()
    $Ctx.ExecuteQuery()
  
    Write-host -ForegroundColor Green "New Item has been added to the List!"
}
Catch {
    write-host -ForegroundColor Red "Error:" $_.Exception.Message
} 

Note

Kindly note that these steps are run in SharePoint 2016 on-premises environment. Same script should work against SharePoint online, but it is required to add SharePoint online CSOM libraries and authenticate it using MSAL. You can refer to connecting to SharePoint online using CSOM in references section.

References