This post is the first in a series detailing the creation of SharePoint Site Columns and Content Types via PowerShell. The series has three parts as follows:

Part 1: Bulk Creating Site Columns with PowerShell

Part 2: Bulk Creating Content Types with PowerShell

Part 3: Bulk Adding Site Columns to Content Types with PowerShell

Bulk Creating Site Columns with PowerShell: Overview
The process of manually creating Site Columns in SharePoint can be a tedious process, especially if you follow best practices and truncate the field name before re-editing the Site Column to allow for a friendly display name. This is done to ensure that the internal field name does not contain any spacing characters such as %20. By counting the clicks, you may quickly realize the onset of Carpal Tunnel and start looking for a more efficient method for creating Site Columns. This is where PowerShell comes to the rescue.

Bulk Creating Site Columns with PowerShell: The Logic
The script in the next section loops through a CSV file in order to parse values into an XML output. The XML output is required to create a Site Column as there is not a PowerShell cmdlet to facilitate the required options. The ForEach statement is used to get each row in the CSV, and was chosen over the ForEach-Object statement as ForEach-Object had issues with its sequential processing of the XML output. While a regular ForEach statement stores the loop into memory before processing, it should not impact a properly sized SharePoint farm. The Site Collection URL is also processed as part of the loop in order to allow the creation of Site Columns across Site Collections, although, most will likely target the Content Type Hub for central control of their Site Columns.

Bulk Creating Site Columns with PowerShell: The Script
The CreateSiteColumns.ps1 script references a CSV file named CreateSiteColumns. Instructions on the save location and execution of the files are included inline with the script, as well as, the allowable file types for Site Columns. You will need to create a CSV file named "CreateSiteColumns" with the column headings as listed in the following table.

Table 1: CreateSiteColumns.csv Column Headings, Description, and Test Values

Column Heading Name

Column Heading Description

Column Heading Test Value

SiteCollectionURL

The Site Collection where the Site Column will be created

http://intranet.yourcompany.com

FieldType

The field type of the Site Column. Use the inline documentation of the script to select the appropriate field type

TaxonomyFieldType

Name

The name of the Site Column

TestMMField

Description

The description of the Site Column

This is a Managed Metadata column

DisplayName

The display name of the Site Column

Test MM Field

StaticName

The internal name of the Site Column

TestMMField

Group

The group where the Site Column is referenced. If no group exists, it will be created

Test Custom Columns

Hidden

Sets the hidden value of the Site Column

FALSE

Required

Sets the required value of the Site Column

TRUE

Sealed

Sets the sealed status of the Site Column

FALSE

ShowInDisplayForm

Determines if the Site Column will be displayed in the display form

TRUE

ShowInEditForm

Determines if the Site Column will be displayed in the edit form

TRUE

ShowInListSettings

Determines if the Site Column will be displayed in the list settings

TRUE

ShowInNewForm

Determines if the Site Column will be displayed in the new form

TRUE


Open notepad and copy the following script into a text file named CreateSiteColumns. Once the file is saved, rename the extension of the file to .ps1 to convert it into a PowerShell script. Please note the inline documentation for the save location and execution instructions.

<span class="co1"># CreateSiteColumns.ps1</span>
<span class="co1">#</span>
<span class="co1"># Description: </span>
<span class="co1">#</span>
<span class="co1"># This script creates Site Columns in the appropriate Site</span>
<span class="co1"># Collections as listed in the CreateSiteColumns.csv. </span>
<span class="co1"># The .CSV needs to be saved to "C:\PowerShell\" directory.</span>
<span class="co1"># If this directory does not exist, you will need to create it.</span>
<span class="co1">#</span>
<span class="co1"># Running this script requires running PowerShell with elevated </span>
<span class="co1"># privileges so right click the SharePoint 2010 Management Shell</span>
<span class="co1"># and select "Run as administrator" then use change directory </span>
<span class="co1"># commands and tabs to run the PS1 from its directory.</span>
<span class="co1">#</span>
<span class="co1"># This script assumes that you are creating the Site Column in </span>
<span class="co1"># the top-level site of a site collection.</span>
<span class="co1"># It can also be used to create a Site Column in the Content Type Hub.</span>
<span class="co1"># </span>
<span class="co1"># Once a Site Column is created, you will need to configure any </span>
<span class="co1"># options not covered in the script, as the varying options per </span>
<span class="co1"># field type would create increased load and may not reduce time </span>
<span class="co1"># entry versus selecting options in a Site Column within the </span>
<span class="co1"># SharePoint Site Settings GUI.</span>
<span class="co1">#</span>
<span class="co1"># LIST OF FIELD TYPES</span>
<span class="co1">#</span>
<span class="co1"># OOTB Field Types</span>
<span class="co1">#</span>
<span class="co1"># AllDayEvent</span>
<span class="co1"># Attachments</span>
<span class="co1"># Boolean</span>
<span class="co1"># BusinessData</span>
<span class="co1"># Calculated</span>
<span class="co1"># Choice</span>
<span class="co1"># Computed</span>
<span class="co1"># ContentTypeId</span>
<span class="co1"># Counter</span>
<span class="co1"># CrossProjectLink</span>
<span class="co1"># Currency</span>
<span class="co1"># DateTime</span>
<span class="co1"># Decimal</span>
<span class="co1"># File</span>
<span class="co1"># GridChoice</span>
<span class="co1"># Guid</span>
<span class="co1"># Integer</span>
<span class="co1"># Lookup</span>
<span class="co1"># LookupMulti</span>
<span class="co1"># ModStat</span>
<span class="co1"># MultiChoice</span>
<span class="co1"># MultiColumn</span>
<span class="co1"># Note</span>
<span class="co1"># Number</span>
<span class="co1"># PageSeperator</span>
<span class="co1"># Recurrence</span>
<span class="co1"># Text</span>
<span class="co1"># ThreadIndex</span>
<span class="co1"># Threading</span>
<span class="co1"># Url</span>
<span class="co1"># User</span>
<span class="co1"># UserMulti</span>
<span class="co1"># WorkflowEventType</span>
<span class="co1"># WorkflowStatus</span>
<span class="co1"># </span>
<span class="co1"># Hold Field Types</span>
<span class="co1">#</span>
<span class="co1"># HoldsField</span>
<span class="co1"># ExemptField</span>
<span class="co1"># </span>
<span class="co1"># Publishing Field Types</span>
<span class="co1">#</span>
<span class="co1"># HTML</span>
<span class="co1"># Image</span>
<span class="co1"># Link</span>
<span class="co1"># SummaryLinks</span>
<span class="co1"># LayoutVariationsField</span>
<span class="co1"># ContentTypeIdFieldType</span>
<span class="co1"># PublishingScheduleStartDateFieldType</span>
<span class="co1"># PublishingScheduleEndDateFieldType</span>
<span class="co1"># MediaFieldType</span>
<span class="co1"># </span>
<span class="co1"># SPRating Field Types</span>
<span class="co1">#</span>
<span class="co1"># AverageRating</span>
<span class="co1"># RatingCount</span>
<span class="co1"># </span>
<span class="co1"># TargetTo Field Types</span>
<span class="co1">#</span>
<span class="co1"># TargetTo</span>
<span class="co1"># </span>
<span class="co1"># Taxonomy Field Types</span>
<span class="co1">#</span>
<span class="co1"># TaxonomyFieldType</span>
<span class="co1"># TaxonomyFieldTypeMulti</span>
 
 
<span class="co1"># Reference the CSV holding the Site Column values and begin the loop</span>
<span class="re0">$create</span> <span class="sy0">=</span> <span class="kw1">Import-Csv</span> <span class="kw5">-path</span> c:\PowerShell\CreateSiteColumns.csv
<span class="kw3">ForEach</span><span class="br0">(</span><span class="re0">$row</span> <span class="kw3">in</span> <span class="re0">$create</span><span class="br0">)</span> <span class="br0">{</span>
 
<span class="co1"># Get Site and Web object</span>
<span class="re0">$site</span> <span class="sy0">=</span> Get<span class="sy0">-</span>SPSite <span class="sy0">-</span>Identity <span class="re0">$row</span>.SiteCollectionURL
<span class="re0">$web</span> <span class="sy0">=</span> <span class="re0">$site</span>.RootWeb
 
<span class="co1"># Assign fieldXML variable with XML string for Site Column</span>
<span class="re0">$fieldXML</span> <span class="sy0">=</span> <span class="st0">'+</span><span class="re0">$row</span>.FieldType<span class="sy0">+</span><span class="st0">'"
 Name="'</span><span class="sy0">+</span><span class="re0">$row</span>.Name<span class="sy0">+</span><span class="st0">'"
 Description="'</span><span class="sy0">+</span><span class="re0">$row</span>.Description<span class="sy0">+</span><span class="st0">'"
 DisplayName="'</span><span class="sy0">+</span><span class="re0">$row</span>.DisplayName<span class="sy0">+</span><span class="st0">'"
 StaticName="'</span><span class="sy0">+</span><span class="re0">$row</span>.StaticName<span class="sy0">+</span><span class="st0">'"
 Group="'</span><span class="sy0">+</span><span class="re0">$row</span>.<span class="kw2">Group</span><span class="sy0">+</span><span class="st0">'"
 Hidden="'</span><span class="sy0">+</span><span class="re0">$row</span>.Hidden<span class="sy0">+</span><span class="st0">'"
 Required="'</span><span class="sy0">+</span><span class="re0">$row</span>.Required<span class="sy0">+</span><span class="st0">'"
 Sealed="'</span><span class="sy0">+</span><span class="re0">$row</span>.Sealed<span class="sy0">+</span><span class="st0">'"
 ShowInDisplayForm="'</span><span class="sy0">+</span><span class="re0">$row</span>.ShowInDisplayForm<span class="sy0">+</span><span class="st0">'"
 ShowInEditForm="'</span><span class="sy0">+</span><span class="re0">$row</span>.ShowInEditForm<span class="sy0">+</span><span class="st0">'"
 ShowInListSettings="'</span><span class="sy0">+</span><span class="re0">$row</span>.ShowInListSettings<span class="sy0">+</span><span class="st0">'"
 ShowInNewForm="'</span><span class="sy0">+</span><span class="re0">$row</span>.ShowInNewForm<span class="sy0">+</span><span class="st0">'">'</span> 
 
<span class="co1"># Output XML to console</span>
<span class="kw1">write-host</span> <span class="re0">$fieldXML</span>
 
<span class="co1"># Create Site Column from XML string</span>
<span class="re0">$web</span>.Fields.AddFieldAsXml<span class="br0">(</span><span class="re0">$fieldXML</span><span class="br0">)</span>
 
<span class="co1"># Dispose of Web and Site objects and close the loop</span>
<span class="re0">$web</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
<span class="re0">$site</span>.Dispose<span class="br0">(</span><span class="br0">)</span>
<span class="br0">}</span>