Friday, March 21, 2014

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 site collection level. When you add a content type to a SharePoint list, its copied from site content type gallery to the list, which means when you customize content type at list level, it doesn't affect the parent content type which lives at site collection level.

In OOPS terminology, you can consider "Content Type" as the class and items created from that content type as "Objects"

How to Associate a Content Types to SharePoint Library or List:
 To add content type to list or Library in SharePoint, do the following:
  • Go to the List / Library settings 
  • Click on Advanced Settings under General Settings group
  • On content types option set it to "Yes" for allow management of content types.
  • Now, in 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.
sharepoint powershell add content type to list
SharePoint 2013 PowerShell add content type to library
  •  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.
    sharepoint 20130 powershell add content type to list
Remove content type from 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.
Content types and site columns are hierarchically inherited! So, if you create a content type, it will be available to all of its subsites underneath it!

Task: Add content type to SharePoint list using PowerShell:
If you want to associate a content type to multiple lists in multiple sites, you can use below PowerShell scripts to do it programmatically! In my case, I've a site with 50+ subsites. 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 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)'"
    }
}

#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 ="http://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

    #To 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!



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...