0

In SPO, i am trying to delete a Managed Metadata Site Column from a library. I'm using the below script to delete:

$SiteURL = "https://Abc.sharepoint.com/sites/SiteName"
$ListName = "Site Pages"
$ColumnName = "CandC_Tax_6" #Internal Name
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Useweblogin
Set-PnPField -List $ListName -Identity $ColumnName -Values @{Required=$False;Hidden=$False} 
Remove-PnPField -Identity $ColumnName -List $ListName -Force   
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

When i execute the script, i get the below error:

"Error: Cannot change Hidden attribute for this field"

I'm able to delete few other columns in the library using this script but it is displaying error when trying to delete this particular Sitecolumn.

2 Answers 2

0

Looks like you can't set this column as hidden for some reason. This is making the rest of the script crash and you end up not being able to remove it.

If you only need to delete this column from the list. ¿Why not remove the part where you change the properties of the column?

$SiteURL = "https://Abc.sharepoint.com/sites/SiteName"
$ListName = "Site Pages"
$ColumnName = "CandC_Tax_6" #Internal Name
Try {
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Useweblogin
Remove-PnPField -Identity $ColumnName -List $ListName -Force   
}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

If the issue is the properties part, you should be able to delete it this way.

3
  • Didn't help, still getting the same error
    – George
    Commented Apr 20, 2023 at 11:40
  • ¿Are you getting "Error: Cannot change Hidden attribute for this field" even after removing the part of the code that changes properties?
    – Vertamin
    Commented Apr 20, 2023 at 12:14
  • yes I'm receiving the same error even after removing Set-PnPField command line
    – George
    Commented Apr 20, 2023 at 12:55
0

If a site column has a value in an item, it will not be possible to delete the site column until the value has been removed from all items that are using the column.


In such a case, you may need to first find and update all the items that are using the site column, remove the column from the list, and then delete the site column. Here's an example PowerShell script that you can use to delete a site column from a list even if it has values in the items:

#Variables
$SiteURL = "https://YourSite.com"
$ListName = "Your List"
$FieldName = "Your Column"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the List and Field
$List = Get-PnPList -Identity $ListName
$Field = Get-PnPField -Identity $FieldName

#Check if Field exists in List
if($List.Fields.Contains($FieldName))
{
    #Get all Items where the Field is not Empty
    $Items = Get-PnPListItem -List $ListName -Fields $FieldName -ScriptBlock 
    {Param($items) $items | where {$_.FieldValues[$FieldName] -ne $null}}

    #If Field is not used in Items
    if($Items.Count -eq 0)
    {
        #Remove Field from List
        Remove-PnPFieldFromList -Identity $Field.Id -List $ListName -Force
        Write-host -f Green "Site Column Deleted from List Successfully!"
    }
    else
    {
        Write-host -f Yellow "Cannot delete the Column as it has values in List Items!"
    }
}
else
{
    Write-host -f Yellow "Column doesn't exist in the List!"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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