SharePoint Online: Change Multiple Lines of Text Field Type to Rich Text using PowerShell
Requirement: Change Multiple Lines of Text Field type to Rich Text in SharePoint Online.
How to Change Multiple Lines of Text Field Type from Plain Text to Rich Text?
Switching from plain text to rich text or enhanced rich text in SharePoint Online is quite simple,
PowerShell to Set Multiple Lines of Text Column type to "Rich Text" in SharePoint Online:
What if you want to convert all multi-line text fields to rich text column in a SharePoint Online site? You can convert multiple lines of text fields in all lists and libraries with this below PowerShell script:
Convert Rich Text Field to Enhanced Rich Text Field using PowerShell:
To make a Multiple lines of text field type to enhanced rich text field, we should convert the field to "Rich Text" first (So that the "RichTextMode" attribute populates) from plain text and then change the field schema.
How to Change Multiple Lines of Text Field Type from Plain Text to Rich Text?
Switching from plain text to rich text or enhanced rich text in SharePoint Online is quite simple,
- Go to the list or site where the target column exists. Get in to the column settings.
- Select the column type to "Rich Text (Bold, italics, text alignment, hyperlinks)" or "Enhanced rich text (Rich text with pictures, tables, and hyperlinks)" as per your requirement.
- Click on "OK" to save your changes.
PowerShell to Set Multiple Lines of Text Column type to "Rich Text" in SharePoint Online:
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Function to Set Multiple Lines of Text Field Type to "Rich Text" Function Set-SPOMultilineTextType([String]$SiteURL,[String]$ListName, [String]$FieldInternalName) { Try { #Get credentials to connect $Cred= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Credentials #Get the list & Fields $List = $Ctx.Web.lists.GetByTitle($ListName) $Ctx.Load($List) $ListFields = $List.Fields $Ctx.Load($ListFields) $Ctx.ExecuteQuery() #Get the Field to Update $FieldToUpdate = $ListFields | Where {$_.InternalName -eq $FieldInternalName} If($FieldToUpdate -ne $NULL) { #Update the Field Settings $FieldToUpdate.RichText = $True $FieldToUpdate.Update() $Ctx.ExecuteQuery() write-host -f Green "Field '$($FieldToUpdate.Title)' Updated in List '$($List.Title)' Successfully!" $_.Exception.Message } } Catch { write-host -f Red "Error Updating Field Settings!" $_.Exception.Message } } #Config Parameters $SiteURL="https://crescenttech.sharepoint.com" $ListName="Projects V1" $FieldInternalName="Project_x0020_Description" #Call the function Set-SPOMultilineTextType -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName
What if you want to convert all multi-line text fields to rich text column in a SharePoint Online site? You can convert multiple lines of text fields in all lists and libraries with this below PowerShell script:
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #To call a non-generic method Load Function Invoke-LoadMethod() { param([Microsoft.SharePoint.Client.ClientObject]$Object = $(throw "Please provide a Client Object"),[string]$PropertyName) $ctx = $Object.Context $load = [Microsoft.SharePoint.Client.ClientContext].GetMethod("Load") $type = $Object.GetType() $clientLoad = $load.MakeGenericMethod($type) $Parameter = [System.Linq.Expressions.Expression]::Parameter(($type), $type.Name) $Expression = [System.Linq.Expressions.Expression]::Lambda([System.Linq.Expressions.Expression]::Convert([System.Linq.Expressions.Expression]::PropertyOrField($Parameter,$PropertyName),[System.Object] ), $($Parameter)) $ExpressionArray = [System.Array]::CreateInstance($Expression.GetType(), 1) $ExpressionArray.SetValue($Expression, 0) $clientLoad.Invoke($ctx,@($Object,$ExpressionArray)) } #Function to convert Multiple Lines of Text Field Type to "Rich Text" Function Set-AllSPOMultilineTextType([String]$SiteURL) { Try { #Get credentials to connect $Cred= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Credentials #Get all lists $Lists = $Ctx.web.Lists $Ctx.Load($Lists) $Ctx.ExecuteQuery() #Iterate through each list ForEach($List in $Lists | Where {$_.Hidden -eq $False}) { #Get AllowDeletion Property to filter system lists Invoke-LoadMethod -Object $List -PropertyName "AllowDeletion" $Ctx.ExecuteQuery() If($List.AllowDeletion -eq $True) { #Get All List Fields $ListFields = $List.Fields $Ctx.Load($ListFields) $Ctx.ExecuteQuery() ForEach($Field in $ListFields) { If($Field.TypeDisplayName -eq "Multiple lines of text" -and $Field.hidden -eq $False -and $Field.ReadOnlyField -eq $False) { #Update the Field Settings $FieldToUpdate.RichText = $True $FieldToUpdate.Update() $Ctx.ExecuteQuery() write-host -f Green "Field '$($Field.Title)' Updated in List '$($List.Title)' Successfully!" $_.Exception.Message } } } } } Catch { write-host -f Red "Error Updating Field Settings!" $_.Exception.Message } } #Config Parameters $SiteURL="https://crescenttech.sharepoint.com" #Call the function Set-AllSPOMultilineTextType -SiteURL $SiteURL
Convert Rich Text Field to Enhanced Rich Text Field using PowerShell:
To make a Multiple lines of text field type to enhanced rich text field, we should convert the field to "Rich Text" first (So that the "RichTextMode" attribute populates) from plain text and then change the field schema.
#Load SharePoint CSOM Assemblies Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" #Function to Set Multiple Lines of Text Rich Text Field Type to "Enhanced Rich Text" Function Set-SPOMultilineRichTextToEnhanced([String]$SiteURL, [String]$ListName, [String]$FieldInternalName) { Try { #Get credentials to connect $Cred= Get-Credential $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password) #Setup the context $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL) $Ctx.Credentials = $Credentials #Get the list & Fields $List = $Ctx.Web.lists.GetByTitle($ListName) $Ctx.Load($List) $ListFields = $List.Fields $Ctx.Load($ListFields) $Ctx.ExecuteQuery() #Get the Field to Update $FieldToUpdate = $ListFields | Where {$_.InternalName -eq $FieldInternalName} If($FieldToUpdate -ne $NULL) { If($FieldToUpdate.RichText -eq $True) { #Enable Enhanced Rich Text $FieldToUpdate.SchemaXml = $FieldToUpdate.SchemaXml.Replace("RichTextMode=`"Compatible`"","RichTextMode=`"FullHtml`"") $FieldToUpdate.Update() $Ctx.ExecuteQuery() write-host -f Green "Field '$($FieldToUpdate.Title)' Updated in List '$($List.Title)' Successfully!" $_.Exception.Message } else { write-host -f Yellow "Field '$($FieldToUpdate.Title)' Should be a Rich Text Field in order to Make it Enhanced Rich Text!" $_.Exception.Message } } else { write-host -f Yellow "Field '$($FieldToUpdate.Title)' doesn't exist in the List!" $_.Exception.Message } } Catch { write-host -f Red "Error Updating Field Settings!" $_.Exception.Message } } #Config Parameters $SiteURL="https://crescenttech.sharepoint.com" $ListName="Projects V1" $FieldInternalName="Project_x0020_Description" #Call the function Set-SPOMultilineRichTextToEnhanced -SiteURL $SiteURL -ListName $ListName -FieldInternalName $FieldInternalName
No comments:
Please Login and comment to get your questions answered!