How to Add a Column to SharePoint List using PowerShell?

Requirement: Add columns to SharePoint list using PowerShell.

PowerShell Script to Add Columns to SharePoint List:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Add-FieldToList($SiteURL,$ListName, $FieldName, $FieldType, $IsRequired)
{
    #Set the Error Action
    $ErrorActionPreference = "Stop"

    Try{
        #Get the List
        $List = (Get-SPWeb $SiteURL).Lists.TryGetList($ListName)
        
        #Check if List with specific name exists
        if($List -ne $null)
        {
            if(!$List.Fields.ContainsField($FieldName))
            {      
                #Add columns to the List 
                $List.Fields.Add($FieldName,$FieldType,$IsRequired)

                #Update the List
                $List.Update()

                #Update the default view to include the new column
                $View = $List.DefaultView # OR $List.Views["All Items"]
                $View.ViewFields.Add($FieldName)
                $View.Update()

                write-host "New Column '$FieldName' Added to the List!" -ForegroundColor Green
            }
            else
            {
                write-host "Field '$FieldName' Already Exists in the List" -ForegroundColor Red
            }
        }
        else
        {
            write-host "List '$ListName' doesn't exists!" -ForegroundColor Red
        }        
    }
     catch {
        Write-Host $_.Exception.Message -ForegroundColor Red
    }
    finally {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    }
}

#Parameters 
$SiteURL="http://intranet.crescent.com/"
$ListName = "Customer Directory"

#Add 'Name' - Single Line of Text Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Text
$FieldName="Name"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

#Add Phone Number - Number Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::Number
$FieldName="Phone Number"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

#Date of Joing - Date Field
$FieldType = [Microsoft.SharePoint.SPFieldType]::DateTime
$FieldName="Date of Join"
$IsRequired = $False
#Call the funtion to Add Field to List
Add-FieldToList $SiteURL $ListName $FieldName $FieldType $IsRequired

You can also add fields to SharePoint list from their field schema. Here is how:
#Field Schemas
$NameFldSchema="<Field Type='Text' DisplayName='Name' Required='False' MaxLength='255' StaticName='Name' Name='Name' />"
$PhoneNoFldSchema="<Field Type='Number' DisplayName='Phone Number' Required='False' MaxLength='255' StaticName='PhoneNumber' Name='PhoneNumber' />"
$DOBFldSchema="<Field Type='DateTime' DisplayName='Date of Birth' Required='False' MaxLength='255' StaticName='DateOfBirth' Name='DateOfBirth' /> "
#For Field schemas, Refer: https://msdn.microsoft.com/en-us/library/office/aa979575(v=office.15).aspx
  
#Add Columns to the List
$List.Fields.AddFieldAsXml($NameFldSchema, $True,[Microsoft.SharePoint.SPAddFieldOptions]::AddFieldToDefaultView)

While the above script is fairly simple, to add other type of fields such as choice fields, lookups, etc.  refer below posts:
How to Add a Column to SharePoint List using PowerShell? How to Add a Column to SharePoint List using PowerShell? Reviewed by Salaudeen Rajack on December 12, 2015 Rating: 5

4 comments:

  1. Hi Salaudeen

    Thanks for this script, it is appreciated.
    I just can't get it to work for a "Person or Group" column.
    I tried:
    $FieldType = [Microsoft.SharePoint.SPFieldType]::Person
    $FieldType = [Microsoft.SharePoint.SPFieldType]::PersonGroup

    Nothing seems to work.
    Thanks again
    Marlin

    ReplyDelete
    Replies
    1. Try the FieldXML Method: $FieldXml = "<Field Type='User' DisplayName='"+ $DisplayName +"' Required='"+ $Required +"' Name='"+ $Name +"'/>"

      Delete
    2. Hello,

      can you explaint how to ad "FieldXML Method: $FieldXml for "Person or Group" to this script in up site?

      I´m new in powershell. I have the same problem with add Person and Group Column and set parameters fields. Thx for help.

      Delete
  2. Thanks mate, all the best!

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.