Add-Remove Content Types to SharePoint List or Library using PowerShell
SharePoint content types enables user to organize related data together. Everything is built around the content types in SharePoint to provide consistency and re-usability. Say for instance, All list templates such as Announcements, Tasks are built with content types “Announcement” and “Task” respectively! Typically a content type consists of template and metadata columns. E.g. Lets consider “Sales Proposal document” content type which may be created at site collection with a template for sales proposal and columns to capture its related meta data, and can be associated with any number of document libraries.
By default, content types are scoped at the site collection level. When you add a content type to a SharePoint list, its copied from the site content type gallery to the list, which means when you customize a content type at list level, it doesn’t affect the parent content type which lives at the site collection level.
How to Associate a Content Types to SharePoint Library or List?
To add content type to a list or Library in SharePoint, do the following:
- Go to the List / Library settings
- Click on Advanced Settings under the General Settings group
- On the content types option, set it to “Yes” to allow management of content types.
- Now, on the list settings page, you’ll find a new section “Content types” with all associated content types with the list.
- You can click on “Add from existing site content types” link to add any existing content type to the list.
- On the “Add Content Types” page, in the “Available Site Content Types” section, select the content types you want to add to the list or library click the Add and then OK. Now the new content type appears in the settings page.
Add a content type to SharePoint list using PowerShell:
If you want to associate a content type to multiple lists on multiple sites, you can use the below PowerShell scripts to do it programmatically! In my case, I have a site with 50+ subsites. The requirement is to add a content type to a particular library on all sites. So, I wrote this PowerShell custom function to add remove content type in the SharePoint library.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
#Custom PowerShell Function to Add Content type to SharePoint list
Function Add-ContentTypeToList([Microsoft.SharePoint.SPList]$List, $ContentTypeName)
{
#Check if Content Type Management is enabled
if($List.ContentTypesEnabled -ne $true)
{
Write-Host "Enabling Content type management on list: $($list.Title)"
$List.ContentTypesEnabled = $true
$List.Update()
}
#Get the content type
$ContentType = $List.ParentWeb.Site.RootWeb.ContentTypes[$ContentTypeName]
#If content type found in the site
if ($ContentType)
{
#Check If list doesn't has the specific content type already
if (-not $list.ContentTypes[$ContentTypeName])
{
#Add content type to the list
$list.ContentTypes.Add($ContentType) | Out-Null
$list.Update()
Write-Host "Content Type Added to the list: $($List.Title)"
}
else
{
Write-Host "Content Type '$($ContentTypeName)' already exists on the list '$($list.Title)'"
}
}
else
{
Write-Host "Cannot find the Content Type '$($ContentTypeName)' to add to list '$($list.Title)'"
}
}
#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposal"
$ContentTypeName ="Sales Proposal"
#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)
if($list -ne $null)
{
#Call the function to add content type
Add-ContentTypeToList $list $ContentTypeName
}
How to Remove content type from a list?
To remove any unused content types that you aren’t using, do the following:
- Browse to the List Settings or Library Settings page.
- Under the “Content Types” section, click the content type you want to remove.
- Click the “Delete This Content Type” link and then click OK at the confirmation prompt.
Remove a content type from the SharePoint list using PowerShell:
#Custom PowerShell Function to Remove Content type from SharePoint list
Function Remove-ContentTypeFromList([Microsoft.SharePoint.SPList]$List, $ContentTypeName)
{
#Check if Content Type Management is enabled
if($List.ContentTypesEnabled -ne $true)
{
Write-Host "Enabling Content type management on list: $($list.Title)"
$List.ContentTypesEnabled = $true
$List.Update()
}
#Get the content to remove from list
$ContentType = $List.ContentTypes[$ContentTypeName]
#If content type found in the site
if ($ContentType)
{
#Check If list doesn't has the specific content type already
if ($list.ContentTypes[$ContentTypeName])
{
#Remove content type from the list
$list.ContentTypes.Delete($ContentType.Id)
$list.Update()
Write-Host "Content Type has been removed from the list: $($List.Title)"
}
else
{
Write-Host "Content Type '$($ContentTypeName)' doesn't exists on the list '$($list.Title)'"
}
}
else
{
Write-Host "Cannot find the Content Type '$($ContentTypeName)' to add to list '$($list.Title)'"
}
}
#Variables for processing
$WebURL ="https://sales.crescent.com"
$ListName ="Q1 Sales Proposal"
$ContentTypeName ="Sales Proposal"
#Get the Web and List
$Web = Get-SPWeb $WebURL
$List = $Web.lists.TryGetList($ListName)
if($list -ne $null)
{
#remove content type from list, call:
Remove-ContentTypeFromList $list $ContentTypeName
}
You can’t remove a content type if items are already created based on the content type! If you try to delete a content type in use, you’ll end up with “Content Type is still in use” error!