Saturday, December 12, 2015

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:



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...