Saturday, February 1, 2014

Discovering SharePoint Content Types and Columns with PowerShell

Content Types and Site Columns are very powerful tools to structure, describe, and administer all types of information in Microsoft SharePoint. It is unfortunate how poorly understood these tools are and how little reference material tends to be available online, particularly for the out-of-the-box content types and site columns available. Thankfully, with PowerShell, you can research your own SharePoint environment and find out nearly anything you need to know.

Note: As of this posting, much of what you can learn and do through PowerShell only applies to SharePoint Server on-prem (i.e., "on premises") or full virtual servers. It does not apply to SharePoint Online, including Office 365. The subset of PowerShell commands available for SharePoint Online has grown some, and hopefully it will continue to grow. Since my team and I help multiple clients with SharePoint planning and architecture, I have access to a development environment at CloudShare, which allows me to run any scripts I would like. A best practice is to never ever run untested scripts in your development environment anyway, so I recommend you have a development environment somewhere, regardless of where your production SharePoint farm resides.

Disclaimer: The script below only reads information from your specified site collection and outputs the results to a CSV file, making no changes to SharePoint. However, run this or any PowerShell scripts at your own risk! I make no guarantees in anyway that this is 100% safe for you. Know what you are doing and use your own judgment. I take no responsibility whatsoever. And other legal stuff...

Let's get on with the PowerShell goodness...

PowerShell Prerequisites

  • You need to have the SharePoint 2013 Management Shell (or SharePoint 2010 if that's what you have) installed, or know how to reference it in your PowerShell scripts.
  • You need administrative rights to a site collection on which to run these scripts.

Get All Available Content Types and Related Columns

There are a number of great blog posts out there on how to do this. My own script here looks for all of the available Content Types and the related Columns, loops through them, and outputs a CSV file with the following columns for each Column (or "field"):
  • Content Type Name
  • Content Type ID
  • Content Type Description
  • Content Type Group
  • Column Title
  • Column Internal Name
  • Column ID
  • Column Group
  • Column Max Length (if applicable)
  • Column Description
To run this script in your own (DEV!) environment, specify the site collection in the "$siteUrl" variable assignment. You should also specify the location and file name of the resulting CSV file in the "$outputFile" variable assignment.

The script:
# Get All Available Content Types and Columns
# By J. Kevin Parker
# www.jkevinparker.com
# Use at your own risk!

# Set your site collection URL here:
$siteUrl = "http://localhost"

# Set the name of the output CSV file here:
$outputFile = "C:\Temp\ContentTypes.csv"

# End of settings

# Create an empty array to fill with objects:
$arrResults = @()

# Get the Site Collection:
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)

# Get the Content Types:
$cts = $site.rootweb.AvailableContentTypes

# Notify:
echo "Retrieving content types and columns..."

# Loop through the content types:
ForEach ($ct in $cts)
{
  # Loop through the Fields (Columns) in the current Content Type:
  ForEach ($field in $ct.Fields)
  {
    # Create a new custom object to hold our row of data with property names:
    $objRow = New-Object PSObject

    # Add our data and property names to the object:
    $objRow | Add-Member -MemberType NoteProperty -Name "CT Name" -Value $ct.Name
    $objRow | Add-Member -MemberType NoteProperty -Name "CT ID" -Value $ct.Id
    $objRow | Add-Member -MemberType NoteProperty -Name "CT Description" -Value $ct.Description
    $objRow | Add-Member -MemberType NoteProperty -Name "CT Group" -Value $ct.Group
    $objRow | Add-Member -MemberType NoteProperty -Name "Col Title" -Value $field.Title
    $objRow | Add-Member -MemberType NoteProperty -Name "Col Internal Name" -Value $field.InternalName
    $objRow | Add-Member -MemberType NoteProperty -Name "Col ID" -Value $field.Id
    $objRow | Add-Member -MemberType NoteProperty -Name "Col Group" -Value $field.Group
    $objRow | Add-Member -MemberType NoteProperty -Name "Col Max Length" -Value $field.MaxLength
    $objRow | Add-Member -MemberType NoteProperty -Name "Col Description" -Value $field.Description

    # Add our object to our array:
    $arrResults += $objRow
  }
}

# Export our results:
$arrResults | Export-Csv $outputFile

# Clean up:
$site.Dispose()

# Notify:
echo "The script finished. The file was saved to: "
echo $outputFile

In another post, I share the information I got from the resulting CSV file in a formatted and usable Excel workbook.

No comments:

Post a Comment