How can I remove an added column from a List Content Type in SharePoint Online via Powershell?

Liam 21 Reputation points
2020-12-16T16:08:03.197+00:00

In a SharePoint Online Document Library, I added several columns, including "Description", to the "Document" list content type (not the site content type). Our business requirements have since altered and the "Description" column needs to be removed from the list content type. On the other columns, there is a "Remove" button, but not on the "Description" column.

I have tried to use PowerShell to access the list content type, as follows:

Connect-PnPOnline -Url https://****.sharepoint.com/sites/***[site name]*** -UseWebLogin
$testlist = Get-PnPList Test
Get-PnPProperty -ClientObject $testlist -property "ContentTypes"
$testlist.ContentType

This brings up a list of the list content types akin to the following:

Name                           Id                             Group                          Description
----                           --                             -----                          -----------
Document                       0x01010**********... Document Content Types         Create a new document.
Folder                         0x01200*********... Folder Content Types           Create a new folder.
Job                            0x0120D52**************... OurCo Content Types             A job...
Document Set                   0x0120D5*******... Document Set Content Types     Create a document set w...

However, if I then try to select one of those list content types to work on, nothing is returned:

$doctype = Get-PnPContentType -Identity Document

I've also tried using $testlist.ContentTypes.getById('0x01010**********') but the table returned is, again, blank.

If I could get a reference to it, perhaps I could then set AllowDeletion on the field, or remove the field via PowerShell. How can I obtain a reference to the list content type?

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

Accepted answer
  1. Echo Du_MSFT 17,316 Reputation points
    2020-12-17T06:55:53.043+00:00

    Hello @Liam ,

    SharePoint Online: Remove a Field from List Content Type using PowerShell

    #Load SharePoint CSOM Assemblies  
    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"  
       
    Function Remove-ColumnFromListContentType()  
    {  
        param  
        (  
            [Parameter(Mandatory=$true)] [string] $SiteURL,  
            [Parameter(Mandatory=$true)] [string] $ListName,  
            [Parameter(Mandatory=$true)] [string] $ContentTypeName,  
            [Parameter(Mandatory=$true)] [string] $ColumnName  
        )  
       
        Try {  
            $Cred= Get-Credential  
            $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)  
       
            #Setup the context  
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)  
            $Ctx.Credentials = $Credentials  
           
            #Get the List  
            $List = $Ctx.Web.Lists.GetByTitle($ListName)  
            $Ctx.Load($List)  
       
            #Get the content type from list  
            $ContentTypeColl = $List.ContentTypes  
            $Ctx.Load($ContentTypeColl)  
            $Ctx.ExecuteQuery()  
               
            #Check if the content type exists in the list         
            $ContentType = $ContentTypeColl | Where {$_.Name -eq $ContentTypeName}  
            If($ContentType -eq $Null)  
            {  
                Write-host "Content Type '$ContentTypeName' doesn't exists in '$ListName'" -f Yellow  
                Return  
            }  
       
            #Get the column to delete from content type  
            $ContentTypeFieldColl =  $ContentType.Fields  
            $Ctx.Load($ContentTypeFieldColl)  
            $Ctx.ExecuteQuery()  
       
            $Field = $ContentTypeFieldColl | Where {$_.Title -eq $ColumnName }  
            if($Field -eq $null)  
            {  
                Write-host "Column '$ColumnName' Doesn't exists in Content Type '$ContentTypeName'" -f Yellow  
            }  
            else  
            {  
                #Get the field link from content type  
                $FieldLinkColl = $ContentType.FieldLinks  
                $Ctx.Load($FieldLinkColl)  
                   
                #Remove field from content type  
                $FieldLink = $FieldLinkColl.GetById($Field.Id)  
                $FieldLink.DeleteObject()  
                $ContentType.Update($false)  
                $Ctx.ExecuteQuery()  
       
                Write-host "Column '$ColumnName' Deleted from '$ContentTypeName' Successfully!" -ForegroundColor Green  
            }  
      }  
        Catch {  
            write-host -f Red "Error Deleting Column from Content Type!" $_.Exception.Message  
        }  
    }  
       
    #Set parameter values  
    $SiteURL="https://test.sharepoint.com/sites/echo"  
    $ListName="doc"  
    $ContentTypeName="Project"  
    #Display Name of the Field  
    $ColumnName="Description"  
       
    #Call the function  
    Remove-ColumnFromListContentType -SiteURL $SiteURL -ListName $ListName -ContentTypeName $ContentTypeName -ColumnName $ColumnName  
    

    Note:

    Get-PnPProperty -ClientObject $testlist -property "ContentTypes"
    AND
    "$doctype = Get-PnPContentType -Identity Document"

    The displayed “Description” is only the Description property of the Content Type, not looks the Description column/filed of the Content Type.

    48979-1.png

    Thanks,
    Echo Du

    ===========

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.