0

I had a content type in my default SharePoint Content Type Hub site (https://tenant.sharepoint.com/sites/contenttypehub) which I deleted before it was completely unpublished. As noted in this post, the content type still exists in the metadata service database and it will keep reappearing in every site I create. How can I ensure this no longer happens?

0

I will advise you to use pnp Add-PnPContentType

link to msdn pnp

0

Solution

The proposed solution is to recreate the content type with the exact same name, parent and ID.

A) Download the SharePoint Online SDK from here and install it.

B) Get the ID and other details of the deleted content type with the following steps:

  1. Go to a site where the content type still exists, then go to the site settings and choose Site Content Types from the Site Settings page (URL is https://tenant.sharepoint.com/sites/sitename/_layouts/15/settings.aspx)

  2. Find the lingering content type from the list and click on it.

  3. From the URL of the lingering content type, get the value of the ctype parameter from the URL. The ID is a hexadecimal code that ends right before an &, which indicates the start of another parameter named Source.

  4. Make sure you also get the Group Name of the content type, which you find at the top of the page.

NOTE: You do not need the parent content type name or ID for this exercise.

C) Creating the PowerShell script The following script is based on the script found here here.

Open PowerShell ISE and copy the following code into the editor:

function New-SPOContentType
{
param(
[Parameter(Mandatory=$true,Position=1)]
        [string]$Username,
        [Parameter(Mandatory=$true,Position=2)]
        $AdminPassword,
        [Parameter(Mandatory=$true,Position=3)]
        [string]$Url,
[Parameter(Mandatory=$true,Position=4)]
        [string]$Description,
[Parameter(Mandatory=$true,Position=5)]
        [string]$Name,
[Parameter(Mandatory=$true,Position=6)]
        [string]$Group,
[Parameter(Mandatory=$true,Position=7)]
        [string]$ParentContentTypeID

        )

  $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)

  $ctx.ExecuteQuery()



  $lci =New-Object Microsoft.SharePoint.Client.ContentTypeCreationInformation
  $lci.Description=$Description
  $lci.Name=$Name 
  $lci.ID="0x01010200551C153681083343B94AC8330CA7BE7F"
  #$lci.ParentContentType=$ctx.Web.ContentTypes.GetById($ParentContentTypeID)
  $lci.Group=$Group

  $ContentType = $ctx.Web.ContentTypes.Add($lci)
  $ctx.Load($contentType)
  try
     {

         $ctx.ExecuteQuery()
         Write-Host "Content Type " $Name " has been added to " $Url
     }
     catch [Net.WebException]
     {
        Write-Host $_.Exception.ToString()
     }



}



    # Paths to SDK. Please verify location on your computer.
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "c:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

    # Insert the credentials and the name of the admin site
    $Username="admingoeshere@CONTOSO.com"
    $AdminPassword=Read-Host -Prompt "Password" -AsSecureString
    $AdminUrl="https://CONTOSO.sharepoint.com/sites/contenttypehub"
    $Description="Something"
    $Name="CONTOSO Marketing Picture"

    # THIS THE ID OF THE PARENT, but it will not reallt be used by the script
    $ParentContentTypeID="0x010102"

    $Group="CONTOSO Content Types"


    New-SPOContentType -Username $Username -AdminPassword $AdminPassword -Url $AdminUrl -Description $Description -Name $Name -Group $Group -ParentContentTypeID $ParentContentTypeID
  1. Change the line $lci.ID= and add the ID of the lingering content type.
  2. Go to the lines under the comment # Paths to SDK [...] and make sure that the path to the SDK DLLs are correct.
  3. Go to the lines under the comment # Insert the credentials and amend them with the details of the lingering content type that you obtained in section B above. Note that you can add anything into $Description, and $ParentContentTypeId. This is because the Parent Content Type ID will in fact be obtained from the ID of the lingering item itself, because they concatenate. But if you are feeling anxious, go get the ID of the parent and add it here :)
  4. Execute the entire script.

NOTE: You must use an account that doesn't use Multi Factor Authentication (MFA). I tried using an account with MFA and it does not work, even if you try using an App Password.

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.