shutterstock_184473665.jpg

Summit 7 Team Blogs

View Site Columns with PowerShell

Problem

Recently, I needed to produce a deployable SharePoint 2010 solution package for a custom Site Column for a client. This wouldn’t have been an issue except for the fact that it needed to exactly replicate the deployed Site Column down to the GUIDs and field definitions. What I needed was some way for the onsite SP administrators to export the currently deployed Site Columns so that I could replicate them within my environment.

Solution

As a result, I produced a short PowerShell script that, when executed on a server within the farm, will produce a listing of XML files for each of the Site Columns within the designated site. Each of these separate XML files will contain the data that is extracted from the SPField.SchemaXml attribute relating to the Site Column. The script is as follows:

# Output-SPSiteColumns.ps1
# Will output a listing of the Schema XML values for each Site Column within a designated site.
#
# Input Parameters
# URL - The Url of the site to pull Site Columns from
# Site Column - The specific name of the Site Column to output, All to output all Site Columns, or List to see all available Site Columns
# Folder Name (Optional) - A specific folder to output the geenerated XML files to.
#
# SharePoint DLL
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

Set-Variable folder -value 'SiteColumns'

function CreateFolder([string] $folderName)
{
if((Test-Path $folderName -pathType container) -eq $False)
{
Write-Host 'Generating ' $folderName ' folder'
New-Item $folderName -type directory
Write-Host ""
}
else
{
Write-Host 'Folder ' $folderName ' already exists, not recreating.'
}
}

# Output the SchemaXml of all Site Columns to individual XML files

# Parse out the folder name if it exists.
if($Args[2] -ne $null)
{
$folder = $Args[2]
}

if($Args[0] -ne $null)
{
Write-Host ""
$url = $Args[0]

$site = new-object Microsoft.SharePoint.SPSite($url)
$webFields = $site.rootweb.Fields

$siteColumn = $Args[1]
if($siteColumn -ne $null)
{
if($siteColumn -eq "List")
{
ForEach ($id in $webFields)
{
Write-Host $id.Title
}
}
elseif($siteColumn -eq "All")
{
CreateFolder $folder
Write-Host "Producing XML for All Site Columns"
ForEach ($field in $webFields)
{
$title = $field.Title
$title = $title -replace "/", "-"
$out = $folder + '' + $title + '.xml'

$field.SchemaXml | Out-File $out
}
}
else
{
$field = $site.rootweb.Fields[$siteColumn]
if($field -ne $null)
{
CreateFolder $folder
$title = $field.Title
Write-Host 'Producing XML for the ' $title 'Site Column.'
$title = $title -replace "/", "-"
$out = $folder + '' + $title + '.xml'
$field.SchemaXml | Out-File $out
}
else
{
Write-Host 'ERROR: Field ' $siteColumn ' does not exist.'
}
}
$site.Dispose()
Write-Host ""
Write-Host "Completed Successfully"
Write-Host ""
}
else
{
Write-Host ""
Write-Host "ERROR: Please specify a valid Site Column, All, or List to see a listing of available Site Columns."
Write-Host ""
}
}
else
{
Write-Host ""
Write-Host "ERROR: Missing URL argument"
Write-Host ""
}

To execute the PowerShell script, there are two required and one optional command line parameters. The execution parameters of the script are as follows:

  • URL – The Url for the site that you would like to view the Site Columns for.
  • Site Column – The specific name of  the Site Column to export, List for a listing of all available Site Columns, or All for an export of all Site Columns within the site
  • Folder Name (Optional) – The folder name which will contain all the Site Column XML export files. By default, these files will be created within a folder named SiteColumns

Using these parameters, the script could be executed based on the following example:

.Output-SPSiteColumns.ps1 http://web.contoso.com/sites/testsite All SiteColumnsExport

Code

You can download the script code file which you to examine and modify as needed. As a standard disclaimer, I assume no responsibility for the execution of this script within your environment.

Enjoy,

Jason

*This item is cross posted at my Summit 7 System blog.

 

Disclaimer
The sample scripts are not supported under any Summit 7 Systems standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Summit 7 Systems further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Summit 7 Systems, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Summit 7 Systems has been advised of the possibility of such damages.

SHARE THIS STORY | |