Friday, April 5, 2013

Removing Content Types and Site Columns from SharePoint

This blog post provides and documents a set of PowerShell scripts we developed to remove a set of content types and their associated site columns from a production SharePoint 2010 farm.

Often SharePoint will throw the exception "Content Type is still in use" when we try to delete a content type we no longer want.

Similarly, when we try to delete a Site Column, SharePoint will throw the exception "Site columns which are included in content types or on lists cannot be deleted. Please remove all instances of this site column prior to deleting it."

I recently helped a client remove a large number of content types as well as the site columns used in those content types from a production SharePoint 2010 farm on-premises. Library templates using these content types had been used extensively, so there were quite a lot of documents, libraries, and sites that all contained references to the content types and site columns. Surgically removing all these content types while leaving in place all the sites, lists, libraries and documents took quite some doing, so I thought I would share with you here some of the knowledge and PowerShell scripts that came from this experience.
 

Background


A SharePoint Content Types is a re-usable set of Site Columns, AKA fields. Content Types are defined in the Site Content Types Gallery in each Site Collection. Content Types are used in Lists and Libraries, as well as other places like workflows. SharePoint will prevent you from deleting a Content Type that is still referenced in a list or library in that site collection. Hence the exception "Content Type is still in use."

Site Columns are also defined in a Gallery in each Site Collection. Lists and Libraries can use these Site Columns directly - without referring to the Content Type. So SharePoint also prevents you from deleting a Site Column if there are any Lists or Libraries using that site column. It throws the exception, "Site columns which are included in content types or on lists cannot be deleted. Please remove all instances of this site column prior to deleting it."

This referential integrity can make it tough to delete a set of site columns and content types without deleting the documents and libraries that use them.  To work back through this chain of dependencies, we followed the sequence of steps listed below to delete content types and site columns from each site collection.

Sequence of Steps


For each step, we provide a link to the PowerShell script (provided on a separate page) that performs that action.

We started by making a list of all the Content Types to be deleted, specifying them in a simple XML file that is one input to the PowerShell scripts.

  1. Remove Content Types from all documents in all document libraries in the Site Collection by running RemoveContentTypesFromDocs.ps1
  2. Remove Content Types from all Libraries in the site collection by running RemoveContentTypesFromLibraries.ps1
  3. Remove Content Types from the Content Type Gallery in the Site Collection by running RemoveContentTypesFromSiteCollection.ps1
If RemoveContentTypesFromSiteCollection encounters exception "Content Type is still in use" you may need to use one of the following scripts which were developed to help locate places where a given content type is still in use within the farm:
Once all the content types are removed, you may start to remove the site columns.

Again, we created a simple XML file list of site columns to be removed

  1. Remove Site Columns from all Libraries by running RemoveSiteColumnsFromLibrariesOneSubsite.ps1 (which just processes one subsite, not the whole colleciton) or RemoveSiteColumnsFromLibraries.ps1 (which process the whole site collection)
  2. Remove Site Columns from the Site Columns Gallery in the Site Collection by running RemoveSiteColumnsFromSiteCollection.ps1
If SharePoint throws exception, "Site columns which are included in content types or on lists cannot be deleted," then run one of the following scripts to search out what Content Type, List or Library may still contain a reference to that site column:

How to Operate the Scripts

All the scripts have a syntax example in the comments near the start of the script.  All the scripts that operate on a site collection take the URL of the site collection as the first input.

The scripts that work with Content Types take, as input, the full pathname of an XML file that provides a list of all the content types to be found or removed.  The file ContentTypes.xml was used for this input file.

Similarly, the scripts dealing with Site Columns take, as input, the full pathname of an XML file that lists the names of all the Site Columns to be found or removed.  This file is named SiteColumns.xml and resides in the same folder as the scripts.

I hope these scripts are helpful and save you some time.