aarebrot.net Frode's blog on Sharepoint and other stuff

Powershell: Retrieve Content Types and Column Fields

Posted on August 25, 2008
VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

So you just found the wicked awesome list of content types and life is good, but you kinda wish you could get a hold of all those content types and column fields that you made yourself. No worries, Powershell is your trusted friend who's always ready to help.

Below are two very basic Powershell scripts. They both connect to a specified site collection, and one exports Content Types while the other exports Column Fields.

These are the exact scripts I used previously and I just whipped them up in about 10 minutes so they're not very fancy. Apparently there's an export to csv function in Powershell, but I didn't know that at the point so I just used the Output-File command.

You can spruce this up if you want, but this should give you the basic functionality to save them in a spreadsheet. The scripts returns ALL content types and column fields, so it'll show both the default ones and the ones that you've created.

# Description
#   Output all available Content Type GUIDs to ContentTypes.csv
#
# Syntax
#   ./listCntTypID
#
# Parameters
#   none
#
# Settings
#   Only change the -value parameter!
#
set-variable -option constant -name url     -value "http://localhost"       # Site collection
set-variable -option constant -name out     -value "ContentTypes.csv"           # Site collection
# End of settings

$site   =    new-object Microsoft.SharePoint.SPSite($url)
$web    = $site.rootweb.ContentTypes
echo "Processing..."
ForEach ($id in $web)
{
'"' + $id.Name + `
'","' + $id.Id + `
'","' + $id.Description + `
'","' + $id.Group + `
'","' + $id.ParentList + `
'"' | Out-File $out -append
}
$site.Dispose()
echo "Finished!"

# Changelog
#
#   v1.0 - August 25th, 2008
#       First release
# Description
#   Output all available Column Field GUIDs to ColumnFields.csv
#
# Syntax
#   ./listColFldID
#
# Parameters
#   none
#
# Settings
#   Only change the -value parameter!
#
set-variable -option constant -name url     -value "http://localhost"       # Site collection
set-variable -option constant -name out     -value "ColumnFields.csv"           # Output file
# End of settings

$site   =    new-object Microsoft.SharePoint.SPSite($url)
$web    = $site.rootweb.Fields
echo "Processing..."
ForEach ($id in $web)
{
'"' + $id.Title + `
'","' + $id.Id + `
'","' + $id.InternalName + `
'","' + $id.MaxLength + `
'","' + $id.Description + `
'","' + $id.Group + `
'","' + $id.TypeShortDescription + `
'"' | Out-File $out -append
}
$site.Dispose()
echo "Finished!"

# Changelog
#
#   v1.0 - August 25th, 2008
#       First release

If you wish you can change the constants to point to the site collection of your choice. You can also specify an alternate name of the output file.

Comments (3) Trackbacks (1)
  1. This is great stuff and I am pulling my hair out on how to bring these two together … a list of all fields on every content type in the site collection. Any thoughts?

  2. Professor Frode-
    Look beautious, but I am stumped on why this error occurs when I run the script substituting my own values for $url and $out:
    Set-Variable : Cannot overwrite variable out because it is read-only or constant.
    That’s all I am changing. Any idea what I could be missing?
    Please and thank you.


Leave a comment