kwizcom banner advertisement

Add Single line of text / Multiple lines of text Fields to SharePoint List using PowerShell

How to Add Single Line of Text or Multiple Lines of Text Columns in SharePoint?
  •  Browse to your list or library in SharePoint >> Click on the List or Library tab from ribbon.
  • Click on "Create Column" button under "Manage Views" group. 
  • In Create Column page, Type the Name for your column. 
  • Select the type of the column as "Single line of text" or "Multiple lines of text" according to your requirement.
    sharepoint powershell add text field
  • In the Additional Column Settings section, provide values for fields such as description, Required Field, Unique, validation, etc. Click OK to save.

How to programmatically Add a Single Line of Text field or Multiple Lines of text fields to SharePoint List using PowerShell:
#configuration parameters
$WebURL="https://portal.crescent.com/"
$ListName="Service Requests"

#Get site and List objects
$web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#Check if List Exists
if($List)
{
    #Setup Field Properties
    $FieldName = "Overall Progress"
    $FieldType = [Microsoft.SharePoint.SPFieldType]::Text
    #For Multiple Lines of Text field, use: $FieldType = microsoft.sharepoint.SPFieldType]::Note
    $IsRequired = $False

    #Add the Field
    $NewField=$List.Fields.Add($FieldName,$FieldType,$IsRequired)
}
else
{
    write-host "List not found!"
}

PowerShell Scripts to add Single line of text to SharePoint List:
Lets make it a reusable function.
#Function to Add a Single Line of Text
Function Add-SPSingleLineOfTextField([Microsoft.SharePoint.SPList]$List, [string]$DisplayName, [string]$Name, [string]$Required)
{
    if(!$List.Fields.ContainsField($DisplayName))
    {
        $FieldXML = "<Field Type='Text' DisplayName='"+ $DisplayName +"' Required='"+ $Required +"' MaxLength='255' Name='"+ $Name +"' />" 
        $NewField=$List.Fields.AddFieldAsXml($FieldXML,$True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
        write-host "New Field Added!" -f Green
    }
    else
    {
        write-host "Field exists already!" -f RED
    }        
}

PowerShell to add Multiple lines of text field to SharePoint List
#Function to Add Multiple Lines of Text
Function Add-SPMultipleLinesOfTextField([Microsoft.SharePoint.SPList]$List, [string]$DisplayName, [string]$Name, [string]$Required, [string]$NumLines, [string]$RichText, [string]$RichTextMode)
{
    if(!$List.Fields.ContainsField($DisplayName))
    {
        #Frame Field XML
        $FieldXML = "<Field Type='Note' DisplayName='"+ $DisplayName +"' Name='"+ $Name +"' Required='"+ $Required +"' NumLines='"+ $NumLines +"' RichText='" + $RichText +"' RichTextMode='"+ $RichTextMode +"' Sortable='FALSE'/>"
        #Add Field
        $NewField=$List.Fields.AddFieldAsXml($FieldXML,$True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)
        write-host "New Multiple Linex of Text Field Added!" -f Green
    }
    else
    {
        write-host "Field exists already!" -f RED
    }        
}

Now, We can call the function to add single line of text:
#configuration parameters
$WebURL="https://portal.crescent.com/"
$ListName="Service Requests"

#Get site and List objects
$web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#Check if List Exists
if($List)
{
    #Call the function to add single line of Text
    Add-SPSingleLineOfTextField $List "Overall Progress" "Progress" $False
}
else
{
    write-host "List not found!"
}

Lets call the function to add Multiple lines of text field:
#configuration parameters
$WebURL="https://portal.crescent.com/"
$ListName="Service Requests"

#Get site and List objects
$web = Get-SPWeb $WebURL
$List = $web.Lists.TryGetList($ListName)

#Check if List Exists
if($List)
{
    #Add Plain Text
    #Add-SPMultipleLinesOfTextField $List "Stage Comments" "comments" $False 6 $False "Compatible"
    
    #Add Rich Text
    Add-SPMultipleLinesOfTextField $List "Stage Comments" "comments" "FALSE" 6 "TRUE" "Compatible"
    
    #Add Enhanced Rich Text
    #Add-SPMultipleLinesOfTextField $List "Stage Comments" "comments" "FALSE" "6" "TRUE" "FullHtml"
}
else
{
    write-host "List not found!"
}
Add Single line of text / Multiple lines of text Fields to SharePoint List using PowerShell Add Single line of text / Multiple lines of text Fields to SharePoint List using PowerShell Reviewed by Salaudeen Rajack on 7:43 PM Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.