Wednesday, December 30, 2015

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

How to programmatically Add a Single Line of Text field or Multiple Lines of text fields in SharePoint 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!"
}



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...