Wednesday, January 21, 2015

Create Site Column in SharePoint using PowerShell

The PowerShell way of adding site column is extremely helpful when you have to add site columns in bulk. Say for e.g. You may have certain site columns created in your DEV environment and would like to have them in Staging or in PROD environment. Will you be creating them one by one with SharePoint UI? No! That would be hectic. Here I'm sharing my PowerShell scripts to create site columns in SharePoint.

Add site column to SharePoint with PowerShell
Here is a simple example of creating site column in SharePoint 2013 with PowerShell:

#Get the Root Web
$RootWeb = Get-SPWeb "http://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 columns using PowerShell - Lets create in Bulk!
Now, lets 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 ="http://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:
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

#Variables
$RootWebURL ="http://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 2010 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 ="http://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 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
Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue

#Variables
$RootWebURL ="http://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: 



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