Create Discussion Board List With Plain Text Body on SharePoint Using CSOM PowerShell

Introduction

In this article, you will learn how to create a discussion list with plain text body, programmatically, using CSOM with PowerShell, on SharePoint 2013 / SharePoint Online.

Note: I was facing issues in changing the field type from rich text to plain text for a created discussion board. So, I am sharing my learning here for the developers to easily overcome this issue. 

Steps Involved

The following prerequisites need to be executed before going for any operations, using CSOM PowerShell on SharePoint sites. 

  • Add the references using the Add-Type command with necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll.
    1. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"    
    2. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"    
  • Initialize the client context object with the site URL.
    1. $siteURL = ""  
    2. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  
  • If you are trying to access SharePoint Online site, then you need to setup the site credentials with credentials parameter and get it set to the client context. If you are trying to access the SharePoint On Premise site, then the credentials parameter is not required to be set to the context. But, you need to run the code on the respective SharePoint server or you need to pass the network credentials and set the context.
    1. #Not required for on premise site - Start    
    2. $userId = ""    
    3. $pwd = Read-Host -Prompt "Enter password" -AsSecureString    
    4. $creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)    
    5. $ctx.credentials = $creds    
    6. #Not required for on premise site - End    
Create List with Plain Text Property
  • Initialize the ListCreationInformation object to create list.
  • Using the object, set the required parameters. The required parameters for creating a list are title and template type. The template type id is 108.
  • Then, add the list creating object to the list collections, on the web.
    1. $listInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation  
    2. $listInfo.Title = "Topics"  
    3. $listInfo.TemplateType = "108"  
    4. $list = $ctx.Web.Lists.Add($ListInfo)  
  • Then, load and execute the query to create the discussions list.
    1. $ctx.Load($list)  
    2. $ctx.ExecuteQuery()  
  • Now, retrieve the fields of list using fields property. Then, retrieve the required field using the field name. Load and execute the query to access the field object.
    1. $fields = $list.Fields  
    2. $field = $fields.GetByTitle("Body")  
    3. $ctx.Load($field)  
    4. $ctx.ExecuteQuery()  
  • TypedObject is used to retrieve, and setting the field text property, number of lines, and other properties. Using the field object, access the rich text property and then set the value. The value type is Boolean.
    • By default, the value of rich text property is true. To make the field as plain text, set false to the property. 
    • If required, change the number of the lines of text. 
    • The following code snippet helps setting the text type to plain text and number of lines to 10.
      1. $field.TypedObject.RichText = $false  
      2. $field.TypedObject.NumberOfLines = 10  
      3. $field.Update()  
      4. $ctx.ExecuteQuery()  

Summary

Thus, you have learned how to create a discussions board list on a SharePoint site and learned how to change the body field property from rich text to plain text, using CSOM with PowerShell on SharePoint 2013 / SharePoint Online sites.