Strange PowerShell behaviour when getting list content type’s ID

For some reasons when retrieving the ID of a content type at the list level in PowerShell, the ID of the parent content type (i.e. the one at the site level) is always returned.

Below is my script:

$web = Get-SPWeb http://myServer
$list = $web.Lists.TryGetList("My List")
$contentType = $list.ContentTypes["My Content Type"]
$contentType.ID

Running the above script would give me:

1 Result

Notice the property being displayed is Parent. I have no idea why it is doing this.

To get around this, I get the content type ID from its SchemaXml property by changing the script to be:

$web = Get-SPWeb http://myServer
$list = $web.Lists.TryGetList("My List")
$contentType = $list.ContentTypes["My Content Type"]
[xml] $contentTypeXml = $contentType.SchemaXml

$contentTypeXml.ContentType.ID

This then gives the expected result as below:

2 Updated Result

This behaviour was observed on SP2013 without any PU or CU applied.

Advertisements

About Bernado

Based in Australia, I am a freelance SharePoint and Dynamics CRM developer. I love developing innovative solutions that address business and everyday problems. Feel free to contact me if you think I can help you with your SharePoint or CRM implementation.
This entry was posted in PowerShell, SharePoint, SharePoint 2013. Bookmark the permalink.

One Response to Strange PowerShell behaviour when getting list content type’s ID

  1. rolandoldengarm says:

    Awesome post. Same is happening in SP2010. Very confusing. Thanks for pointing this out.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s