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

Using PowerShell to retrieve Content Types and associated Fields

Posted on February 28, 2011
VN:F [1.9.22_1171]
Rating: 5.0/5 (4 votes cast)

I’ve previously posted PowerShell scripts that allow you to extract Content Types or Fields from a SharePoint site. But last week a commenter by the named Jeff posed a valid question. What if I want to extract all Content Types in a SharePoint site, and show the associated Fields for each of them?

While my previous scripts does not allow for that, it wasn’t all that hard to combine the two scripts into one. Over the weekend I quickly threw something together and the result is a PowerShell script that iterates through each Content Type and prints out each Field it finds. It’s pretty simple. I’ve posted the script below, and I’ll also post a truncated version of the output for 2007 and 2010 in later blog posts.

# Description
#   Output all available Content Type GUIDs and their respective fields
#
# Syntax
#   ./listCtFields
#
# 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)
$cts = $site.rootweb.ContentTypes
echo "Processing..."

'"CT Name"' + `
',"CT ID"' + `
',"CT Description"' + `
',"CT Group"' +
',"Field Title"' + `
',"Field Internal Name"' + `
',"Field ID"' + `
',"Field Group"' + `
',"Field Max Length"' + `
',"Field Description"' | Out-File $out

ForEach ($id in $cts)
{
  ForEach ($field in $id.Fields)
  {
    '"' + $id.Name + `
    '","' + $id.Id + `
    '","' + $id.Description + `
    '","' + $id.Group + `
    '","' + $field.Title + `
    '","' + $field.InternalName + `
    '","' + $field.Id + `
    '","' + $field.Group + `
    '","' + $field.MaxLength + `
    '","' + $field.Description + `
    '"' | Out-File $out -append
  }
}

$site.Dispose()

echo "Finished!"

# Changelog
#
#   v1.0 - February 28, 2011
#       First release
Using PowerShell to retrieve Content Types and associated Fields, 5.0 out of 5 based on 4 ratings
Comments (11) Trackbacks (0)
  1. awesome, thank you so much.

  2. Great script. Thanks very much

  3. I am new to powershell and I need to do exactly this. How do I modify to work for me?

    • On the very first set-variable line, change “http://localhost” to the URL of the site collection you wish to extract the content types from. Then run the script on a server in your farm. It should spit out a csv file containing all the information.

  4. Hi,
    Great Script. I have a need to insert content types. Is there a way to insert Content Types/Fields using PowerShell into Sharepoint 2007 (MOSS)?

    Thank You.

  5. How do you run this script in MOSS 2007 since there is no Powershell?

    • PowerShell is a separate install on MOSS2007. Just download it, install, and the script should run just fine. That’s what I did for the 2007 version of my data dump.

  6. Hi,

    Can you tell me if it is also possible to pull a specific attribute of a content type using your script (i.e. the Inherits = True/False attribute)? How would this be done?

    Cheers,
    Aaron

    • I don’t see why not… In the inner for loop just put an if statement checking whether a certain field matches your expectations, when only write out the content type if it does.

  7. can we get all custom content types in use in a web application level


Leave a comment

 

No trackbacks yet.