This post is the third 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 Adding Site Columns to Content Types with PowerShell: Overview
While the previous two posts focused to the creation of Site Columns and Content Types, one final step remains; the addition (or linking) of Site Columns to the appropriate Content Types. If you evaluate the amount of clicks it takes to manually add Site Columns for use by a Content Type, then multiply that by your total Content Types, it is clear to see how PowerShell can greatly ease this tedious task.

Bulk Adding Site Columns to Content Types with PowerShell: The Logic
The script in the next section loops through a CSV file in order to determine which Site Columns should be added to each Content Type. The ForEach-Object statement is used to get a row in the CSV and add a Site Column, before proceeding to the next row for processing. This reduces the speed of processing, but also reduces the amount of memory usage during the operation. The Site Collection URL is also processed as part of the loop in order to allow the addition of Site Columns to Content Types across Site Collections, although, most will likely target the Content Type Hub for central control of their Content Types.

Bulk Adding Site Columns to Content Types with PowerShell: The Script
The AddSiteColumnsToContentTypes.ps1 script references a CSV file named AddSiteColumnsToContentTypes. Instructions on the save location and execution of the files are included inline with the script. You will need to create a CSV file named "AddSiteColumnsToContentTypes" with the column headings as listed in the following table.

Table 1: AddSiteColumnsToContentTypes.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 added to the Content Type

http://intranet.yourcompany.com

ContentType

The name of the Content Type that will have the Site Column added to it

Test Document

SiteColumn

The name of the Site Column that will be added to the Content Type

Test Date


Open notepad and copy the following script into a text file named AddSiteColumnsToContentTypes. 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"># AddSiteColumnsToContentTypes.ps1</span>
<span class="co1"># </span>
<span class="co1"># Description: </span>
<span class="co1">#</span>
<span class="co1"># This script adds Site Columns to the appropriate Content </span>
<span class="co1"># Types as listed in the AddSiteColumnsToContentTypes.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 commands </span>
<span class="co1"># and tabs to run the PS1 from its directory.</span>
 
 
<span class="co1"># Reference the CSV holding the Content Type 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\AddSiteColumnsToContentTypes.csv
<span class="re0">$create</span> <span class="sy0">|</span> <span class="kw1">ForEach-Object</span> <span class="br0">{</span>
 
<span class="co1"># Get the Site where the Site Columns will be added to Content Types</span>
<span class="re0">$site</span> <span class="sy0">=</span> Get<span class="sy0">-</span>SPSite <span class="sy0">-</span>Identity <pre class="de1"><span class="co1"># AddSiteColumnsToContentTypes.ps1</span>
<span class="co1"># </span>
<span class="co1"># Description: </span>
<span class="co1">#</span>
<span class="co1"># This script adds Site Columns to the appropriate Content </span>
<span class="co1"># Types as listed in the AddSiteColumnsToContentTypes.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 commands </span>
<span class="co1"># and tabs to run the PS1 from its directory.</span>
 
 
<span class="co1"># Reference the CSV holding the Content Type 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\AddSiteColumnsToContentTypes.csv
<span class="re0">$create</span> <span class="sy0">|</span> <span class="kw1">ForEach-Object</span> <span class="br0">{</span>
 
<span class="co1"># Get the Site where the Site Columns will be added to Content Types</span>
<span class="re0">$site</span> <span class="sy0">=</span> Get<span class="sy0">-</span>SPSite <span class="sy0">-</span>Identity #####replaceparse0#####lt;span class="br0">(</span><a><span class="kw6">$_</span></a>.<span class="st0">'SiteCollectionURL'</span><span class="br0">)</span>
<span class="re0">$web</span> <span class="sy0">=</span> <span class="re0">$site</span>.RootWeb
 
<span class="co1"># Add Site Columns to Content Types</span>
<span class="re0">$ct</span><span class="sy0">=</span><span class="re0">$web</span>.ContentTypes<span class="br0">[</span><span class="st0">"$($_.'ContentType')"</span><span class="br0">]</span>; 
<span class="re0">$fieldAdd</span><span class="sy0">=</span><span class="re0">$web</span>.Fields<span class="br0">[</span><span class="st0">"$($_.'SiteColumn')"</span><span class="br0">]</span>
<span class="re0">$fieldLink</span><span class="sy0">=</span><span class="kw1">New-Object</span> Microsoft.SharePoint.SPFieldLink<span class="br0">(</span><span class="re0">$fieldAdd</span><span class="br0">)</span>
<span class="re0">$ct</span>.FieldLinks.Add<span class="br0">(</span><span class="re0">$fieldLink</span><span class="br0">)</span>;
<span class="re0">$ct</span>.Update<span class="br0">(</span><span class="br0">)</span>
 
<span class="co1"># Dispose of the 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></pre>lt;span class="br0">(</span><a><span class="kw6">$_</span></a>.<span class="st0">'SiteCollectionURL'</span><span class="br0">)</span>
<span class="re0">$web</span> <span class="sy0">=</span> <span class="re0">$site</span>.RootWeb
 
<span class="co1"># Add Site Columns to Content Types</span>
<span class="re0">$ct</span><span class="sy0">=</span><span class="re0">$web</span>.ContentTypes<span class="br0">[</span><span class="st0">"$($_.'ContentType')"</span><span class="br0">]</span>; 
<span class="re0">$fieldAdd</span><span class="sy0">=</span><span class="re0">$web</span>.Fields<span class="br0">[</span><span class="st0">"$($_.'SiteColumn')"</span><span class="br0">]</span>
<span class="re0">$fieldLink</span><span class="sy0">=</span><span class="kw1">New-Object</span> Microsoft.SharePoint.SPFieldLink<span class="br0">(</span><span class="re0">$fieldAdd</span><span class="br0">)</span>
<span class="re0">$ct</span>.FieldLinks.Add<span class="br0">(</span><span class="re0">$fieldLink</span><span class="br0">)</span>;
<span class="re0">$ct</span>.Update<span class="br0">(</span><span class="br0">)</span>
 
<span class="co1"># Dispose of the 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>