Create Site Column in SharePoint using PowerShell

Site columns are reusable fields that you can add to your SharePoint sites, and they can be used in any number of lists or libraries! The PowerShell way of adding site columns is extremely helpful when you have to add site columns in bulk. Say, You may have certain site columns created in your DEV environment and would like to have them in STAGING or a PROD environment. Will you be creating them one by one with SharePoint UI? No! That would be hectic. I’m sharing my PowerShell scripts to create site columns in SharePoint in this post.

Add a Site Column to SharePoint with PowerShell

Here is a simple example of creating a site column in SharePoint 2016 with PowerShell:

#Get the Root Web
$RootWeb = Get-SPWeb "https://intranet.crescent.com"

#Create a text Field site column
$RootWeb.Fields.Add("ProjectCode","Text",$false)
$SalaryField = $RootWeb.Fields.Add("Crescent-Salary", "Currency", $false)

Create Site Column in SharePoint using PowerShell

Create site columns using PowerShell – Let us create in Bulk!

Now, let’s add site columns in bulk. Just enter your column names to the array: $SiteColumnsToAdd and run the code.  

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$RootWebURL ="https://intranet.crescent.com"

#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL

# List of Site Column Names to be Added
$SiteColumnsToAdd = @("Branch Name", "Branch Code", "Destination")

ForEach($SiteColumn in $SiteColumnsToAdd)
{
    if (!$RootWeb.Fields.ContainsField($SiteColumn))
    {
        #Remove Space in the Field's Internal Name
        $InternalName= $SiteColumn.Replace(" ","")
        #Add new site column
        $RootWeb.Fields.Add($InternalName, "Text", $false)

        #Get the site column
        $Addedcolumn = $RootWeb.Fields.GetField($InternalName)
        #Set the Column Title and Group
        $Addedcolumn.Group="Crescent Columns"
        $Addedcolumn.Title=$SiteColumn
        $Addedcolumn.update()

        Write-host "Site column $($SiteColumn) has been Added Successfully!" 
    }
 } 
$RootWeb.Dispose()

SharePoint 2013 PowerShell to create site column – Choice Field:

Here is another example of PowerShell to create a site column in SharePoint:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$RootWebURL ="https://intranet.crescent.com"

#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL

#Create a Choice Field site column
$RootWeb.Fields.Add("Departments","Choice",$false)

#Get the field by internal Name
$ChoiceField=$RootWeb.Fields.GetField("Departments")

#Set the Site column Group
$ChoiceField.Group="Crescent Columns"

#Add Choices
$ChoiceField.Choices.Add("Operations")
$ChoiceField.Choices.Add("Marketing")
$ChoiceField.Choices.Add("Sales")
$ChoiceField.Choices.Add("Finance")

#Set Title
$ChoiceField.Title="Department Selection"
#Default choice
$ChoiceField.DefaultValue="Sales"
#Allow fill in choices
$ChoiceField.FillInChoice=$true

#Apply changes to the column
$ChoiceField.Update()

Write-host "Site column $($SiteColumn) has been Added Successfully!" 
$RootWeb.Dispose()

SharePoint 2016 create site column using PowerShell – using Field Schema XML Method

The AddFieldAsXml method gives us more flexibility:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$RootWebURL ="https://intranet.crescent.com"

#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL

#Define the Site column XML
$FieldXML = '<Field Type="Number" DisplayName="Bpt-InvoiceNo" Required="TRUE" EnforceUniqueValues="FALSE" Indexed="FALSE" Min="1" Max="10000" Group="Bpt Columns" ID="{b81c7da6-1317-46fa-a32b-9f446c30b6e9}" StaticName="Bpt-InvoiceNo" Name="Bpt-InvoiceNo"></Field>'

#Create site columns with powershell from XML
$RootWeb.Fields.AddFieldAsXml($FieldXML)

Write-host "Site column has been Added Successfully!" 

Refer to the Field Element XML to specify field options such as Read-Only, Group, Etc.
https://msdn.microsoft.com/en-us/library/office/ms437580.aspx

SharePoint add site column with PowerShell from CSV File

How about adding site columns in bulk from a CSV file?

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$RootWebURL ="https://intranet.crescent.com"
$CSVPath = "D:\SiteColumns.csv"

#Get the Root Web
$RootWeb = Get-SPWeb $RootWebURL

#Import from CSV File
$SiteColumnsCSV = Import-csv -Path $CSVPath #CSV has "Title" column Header

foreach($Column in $SiteColumnsCSV)
{
 #Get the field title from CSV
        $FieldTitle = $Column.Title
     
    #Check if a site column exists already
    if (!$RootWeb.Fields.ContainsField($FieldTitle))
    {
     #Frame FieldXMLString from the column Title
     $FieldXMLString = [String]::Format('<Field Type="Text" Name="{0}" DisplayName="{1}" StaticName="{0}" Required="FALSE"></Field>',$fieldTitle.Replace(" ",""),$FieldTitle)

     #create site column sharepoint 2010 powershell from XML string
     $RootWeb.Fields.AddFieldAsXml($FieldXMLString)

        Write-host "Site column '$($FieldTitle)' has been Added Successfully!" 
    }
}

$RootWeb.Dispose() 

Related Posts: 

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

Your email address will not be published.