Content Type Hub | Duplicate Content Type Error "A content type with the name already exists"

Content types are one of the best things to happen for SharePoint as they allow better standardization and governance of content.

I was recently hit by a strange content type issue. The client had a SharePoint setup with a content type hub where he built all his root content types and would creating child content types using the parent content types from the hub. The client noted that they couldn't create a child content type for a specific parent content type in the hub. In fact they get an error as soon as they try creating one.

The error message showed:

Duplicate content type - A content type with this name already exists.

I had a look at the list of available content types and none had the same name. What was it?

After quite a bit of research and looking at schemas, databases, etc.  The content database has a field on the dbo.ContentTypes table called “NextChildByte” and that is what lets SharePoint know what that next numerical value should be. This is the heart of our problem. Sometimes SharePoint gets out of sync with itself, and “NextChildByte” indicates a value that already is in use. SharePoint is not intelligent enough to continue looking sequentially until it finds the next available byte sequence. Instead, it throws the error of duplicate content type.

To fix this I had to run the below code:

 function FixContentTypeForChildCreation
    #Grab the ID of the CT you need to fix for child creation
    $ctID = $ct.ID.ToString();
    for($i=0;$i -lt 10; $i++)
        #Create a new child content type with a unique guid and then delete it
        $newCTID = new-object Microsoft.SharePoint.SPContentTypeId($ctID + "00" + [Guid]::NewGuid().ToString("n"));
        $newCT = new-object Microsoft.SharePoint.SPContentType($newCTID, $web.ContentTypes, "Temp Content Type - Auto Deleted");
        $addedCT = $web.ContentTypes.Add($newCT);
    Write-Host "Content Type Fix for Child Creation is Complete"