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.
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.
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"
Initialize the client context object with the site URL.
$siteURL = ""
$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.
# Not required for on premise site - Start
$userId = ""
$pwd = Read-Host -Prompt "Enter password" -AsSecureString
$creds = New Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)
$ctx.credentials = $creds
# 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.
$listInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$listInfo.Title = "Topics"
$listInfo.TemplateType = "108"
$list = $ctx.Web.Lists.Add($ListInfo)
Then, load and execute the query to create the discussions list.
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.
$fields = $list.Fields
$field = $fields.GetByTitle("Body")
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.
a. By default, the value of rich text property is true. To make the field as plain text, set false to the property.
b. If required, change the number of the lines of text.
c. The following code snippet helps setting the text type to plain text and number of lines to 10.
$field.TypedObject.RichText = $false
$field.TypedObject.NumberOfLines = 10
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.