Thursday, December 29, 2011

Remove column from content type using powershell script

http://get-spscripts.com/2010/10/cant-remove-site-column-from-content.html


The example below will remove the “Aliases” site column from the “Sales Document” site content type in http://portal:




#Attach to the web and content type

$web = Get-SPWeb http://portal

$ct = $web.ContentTypes["Sales Document"]



#Get link to the columnn from the web

$spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink ($web.Fields["Aliases"])



#Remove the column from the content type and update

$ct.FieldLinks.Delete($spFieldLink.Id)

$ct.Update()



#Dispose of the web object

$web.Dispose()

Note that any columns of this type deleted from a parent content type will not automatically disappear in the child content types below it. The easiest way to get around this is to use the same script above to remove the site column from child content types, too.




Unfortunately, you may also find that if the content types containing this site column were attached to lists, then the lists themselves will also have the column added to them – even after removing the column from all associated content types. If this is the case, you will not be able to remove the column in the list using the browser UI as the Remove button will be missing on the column administration page here also.



To resolve this, you can either decide to remove the column using PowerShell one list at a time, or if there is a way of defining a batch of lists, use a script to modify multiple lists in one go. The script below provides you with an example of how to walk through each document library of a specified name on each site of the site collection and delete the offending column. If you do decide to run a script like this, please ensure you have fully tested it in a development environment beforehand due to the potential damage that it could inflict:



#Delete column on a specified list in all sites of a site collection

$site = Get-SPSite http://portal

$site
Get-SPWeb -Limit all
ForEach-Object {

#Specify list which contains the column

$list = $_.Lists["Pages"]

#Specify column to be deleted

$field = $list.Fields["Aliases"]

#Allow column to be deleted

$field.AllowDeletion = $true

#Delete the column

$field.Delete()

#Update the list

$list.Update()

}

$site.Dispose()

No comments:

Post a Comment