0

We have a lot of InfoPath forms deployed on our SharePoint 2013 farm as custom Content Types. Over the period of time there are now a lot of such unused custom content types in gallery. When i say a lot of, i mean around 60-80 in count.

I have to delete these unused content types to make the environment cleaner. I know that i can go to content type gallery and start deleting individual content types. But, i don't want to repeat the steps 70-80 times. So, i am looking for some trick to do this quickly, if possible.

Thanks in advance.

2

You can delete the content type via Powershell. Please create the XML file with required Content Types you want to delete and give input for the below powershell script.

param(
    [string] $siteCollectionURL = $(Throw "-- You must specify the URL of a site collection as parameter 1."), #required parameter
 [string] $xmlFilePath = $(Throw "-- You must specify the pathname of an XML file as parameter 2.") #required parameter
)
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction "SilentlyContinue"
Write-host "RemoveContentTypesFromSiteCollection: Processing Site Colleciton: " $siteCollectionURL -BackgroundColor Yellow -ForegroundColor Black
$siteScope = Start-SPAssignment
$web = get-SPWeb $siteCollectionURL 
# get the content types XML from the input XML file
$XmlFileInput = [xml](Get-Content($xmlFilePath))

# loop over content types listed in the input XML file
foreach( $ctElement in $XmlFileInput.ContentTypes.ContentType) 
{
    $contentTypeToRemove = $ctElement.Name

    write-host "  Checking for Content Type " $contentTypeToRemove -ForegroundColor Gray

    $ct = $web.ContentTypes[$contentTypeToRemove]
    if ($ct)
    {
        write-host "    Found Content Type '" $contentTypeToRemove "' in site " $web -ForegroundColor Gray
        try
        { 
            if ($ct.Readonly -eq $true)
            {
                $ct.ReadOnly = $false
                $ct.Update()
            }           
            $ct.Delete()
            $web.Update()  # may not be necessary
            write-host "    Deleted Content Type '" $contentTypeToRemove "' from site " $web -ForegroundColor Black
        }
        Catch
        {
            Write-host "Exception :" $_.exception.message -ForegroundColor red
            Write-host "Web       :" $Web.url -ForegroundColor red
            Write-host "Cont Type :" $contentTypeToRemove -ForegroundColor red
        }
    }
}  
Write-host "RemoveContentTypesFromSiteCollection: Done"

Please verify and let me know if you have any queries on this.

0

There's no easy way to delete them while in use on a list, but if not, Powershell could be very useful.

$sitename = $args[0]
$contentType = $args[1]

$web = Get-SPWeb $sitename
$ct = $web.ContentTypes[$contentType]

if ($ct) {
$ctusage = [Microsoft.SharePoint.SPContentTypeUsage]::GetUsages($ct)
  foreach ($ctuse in $ctusage) {
    $list = $web.GetList($ctuse.Url)
    $contentTypeCollection = $list.ContentTypes;
    $contentTypeCollection.Delete($contentTypeCollection[$ContentType].Id);
    Write-host "Deleted $contentType content type from $ctuse.Url"
    }
$ct.Delete()
Write-host "Deleted $contentType from site."

} else { Write-host "Nothing to delete." }

$web.Dispose()

Here you could find a basic explanation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.