How to delete a hidden library field in SharePoint

Deleting a hidden library field in SharePoint

Hidden fields do not appear in the user interface when viewing Library settings. One reason to delete a hidden field is after a field is removed from a Content Type in a syndication hub, the field remains in the libraries associated with the original Content Type, although it is now “orphaned” and unrelated to the Content Type. The solution is to remove the field itself from each and every library. The script commands below can easily action all libraries based on an iteration through all Web Apps, Sites, Webs and Libs.

A hidden field can be deleted via PowerShell, but one should note a few obstacles to work around. First, the field needs to be unhidden, albeit temporarily. Then the field object needs to be updated. Only then can the field be deleted. Here’s how:

First, let’s get the Web, the list within it, and the target field:

$JPWeb = Get-SPWeb "http ://sharepoint/div/Path/Site"
$lists = $JPWeb.lists
$list = $lists["MyList"]
$fields=$list.fields

Let’s have a peek at the friendly name and internal name of each field in the list, by piping to a select:

$fields | select title, staticname

Let’s grab the target field, and start by unhiding it, updating the field, then deleting it:

$field = $fields["FieldToDelete"]
#$field.delete() #can't delete a hidden column
$field.set_Hidden($false)
$field.Update()
$field.delete() 

The above code has a bit of a risk. External field names can change. Oddly a number of internal field names have the same external name. These are known as “Title” values in the object model. The best approach when deleting a field is to use the Static name. Here’s how:

$field = $list.Fields.GetFieldByInternalName("InternalFieldName")

Best practice is to use a try/catch, as the above function will throw an error if the field is not found.

That’s it. No further updates are necessary to the List or Web objects. If you have content types referencing this field, the local copy of the content type within the List is customized via the removal of this field.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *