PowerShell script to create Bulk Site Columns in SharePoint
We will be using the CSV source to create multiple or bulk site columns in SharePoint.
1) Create a csv file as source. In the below format
2) Create a new powershell script with below code
1) Create a csv file as source. In the below format
Name,Description,Type,Group,Hidden,ShowInDisplayForm,ShowInEditForm,ShowInNewForm,Termset,Term,Choices,DefaultValue,AllowMultipleValues,Lookup,IsTermGroup Bool Col,,Boolean,MyColGp,FALSE,TRUE,TRUE,TRUE,,,,,,,NO Choice Col,,Choice,MyColGp,FALSE,TRUE,TRUE,TRUE,,,"Chice1,choice2",,,,NO Date Col,,DateTime,MyColGp,FALSE,TRUE,TRUE,TRUE,,,,,,,NO Managed Col,,Managed Metadata,MyColGp,FALSE,TRUE,TRUE,TRUE,TermsetName,Term,,,,,NO Description,,Note,MyColGp,FALSE,TRUE,TRUE,TRUE,,,,,,,NO Text Col,,Text,MyColGp,FALSE,TRUE,TRUE,TRUE,,,,,,,NO Link,,URL,MyColGp,FALSE,TRUE,TRUE,TRUE,,,,,,,NO Col2,,User,MyColGp,FALSE,TRUE,TRUE,TRUE,,,,,,,NO
2) Create a new powershell script with below code
#This script creates site columns from the given csv file #Author: Prem Kumar #Date: Feb 11 2015 param( [string]$rooturl = $(Read-Host -prompt "Enter Web Application Root URL...?"), [string]$listname = $(Read-Host -prompt "Enter the list name...?"), [string]$csvpath = $(Read-Host -prompt "Enter the CSV Path...?") ) try { $osite = Get-SPWeb $rooturl $olist= $osite.Lists[$listname] $columnstoadd= Import-Csv $csvpath $oView = $oList.DefaultView $oView.ViewFields.View.ViewFields.DeleteAll() $oView.ViewFields.Add("LinkTitle") write-host "Adding Columns to the list $listname" write-host "Loading Columns data from CSV $csvpath" foreach($column in $columnstoadd) { if($olist.Fields[$column.ColumnName] -eq $null) { $type=$column.DataType if($type -eq "Choice") { $choiceFieldChoices = @($column.Choices.Split(",")) # Declare a new empty String collection $stringColl = new-Object System.Collections.Specialized.StringCollection # Add the choice fields from array to the string collection $stringColl.AddRange($choiceFieldChoices) # Create a new choice field and add it to the web using overload method $spFieldType = [Microsoft.SharePoint.SPFieldType]::$type $olist.Fields.Add($column.ColumnName,$spFieldType,$false,$false, $stringColl) # Update the list $olist.DefaultView.ViewFields.add($column.ColumnName) $olist.Update() write-host "Field added $column" } else { $olist.Fields.Add($column.ColumnName,[Microsoft.SharePoint.SPFieldType]::$type,$false) $olist.DefaultView.ViewFields.add($column.ColumnName) $olist.Update() write-host "Field added $column" } } else { write-host "Column $column already exists in list $listname" } $oView.ViewFields.Add($column.ColumnName) $oView.Update() } } catch { write-host "Exception in Adding List Columns $_.Exception.Message" } finally { write-host "Exit of Add List Columns..." $osite.Dispose() }
Comments
Post a Comment