Adding Content Types using PowerShell


Niklas Goude
-Expert in Residence

You can use Windows PowerShell to add Content Types to a Web site in SharePoint 2010. You can start by retrieving a specific Web site:

PS > $spWeb = Get-SPWeb http://SP01.powershell.nu

You can use the Available Content Types property to see which Content Types are available on your Web site. You’ll also need to retrieve a Content Type from which the one you are creating will be derived:

PS > $spWeb.AvailableContentTypes | Select Name

Next, you can store a Parent Content Type in a variable:

PS > $parent = $spWeb.AvailableContentTypes[Document]

Next, you can initialize a new instance of the SP Content Type class by specifying the Parent Content Type, the collection to which the content type will be added and the name of the new content type:

PS > $contentType =  New-Object Microsoft.SharePoint.SPContentType `
>> -ArgumentList @($parent, $spWeb.ContentTypes, Standard Documents)

You can also set additional properties, such as Group and Description:

PS > $contentType.Group = My Content Type
PS > $contentType.Description = Standard Documents

When you’re ready, you can use the Add() method to add the new content type to the Web site Content Types:

PS > $spWeb.ContentTypes.Add($contentType)
PS > $spWeb.Update()
PS > $spWeb.Dispose()

Twitter This Tip! ReTweet this Tip!


Posted Aug 24 2011, 06:00 AM by sos
Idera SharePoint Reviews SMBology, Inc. Rackspace Hosting Sponsored by Idera and SharePoint Reviews and SMBology and EPCGroup.net
Copyright 2011 SecretsOfSharePoint.com. All rights reserved.