Pages

Wednesday, April 25, 2012

Removing Columns for Content Types

Creating a new custom content type? Have to inherit from something but you don't want to include all the fields/columns from the parent?

Simple (in most cases). Use the following powershell script to remove (note does not delete the column which doesn't belong to you to delete anyway :))

$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"}
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

#Attach to the web and content type
$web = Get-SPWeb http://intranet.contoso.com/
$ct = $web.ContentTypes["Content Type"]

#Insert name of column to remove from Content Type
$columnName = "Comments"

#Get link to the columnn from the web
$spFieldLink = $ct.FieldLinks[$columnName]
#Recently I tried to use this script and this line failed, I replaced it with the one above and its fine.
#$spFieldLink = New-Object Microsoft.SharePoint.SPFieldLink($web.Fields[$columnName])

#Remove the column from the content type and update
$ct.FieldLinks.Delete($spFieldLink.Id)
$ct.Update()

#Test that the column was successfully removed from the Content Type
if ($ct.Fields[$columnName])
{
    Write-Host "$columnName was not removed"
}
else
{
    Write-Host "$columnName was removed"
}

Note: Only issue I have found so far is that I still cannot remove all fields that I want removed. In my case I wanted to remove the "Comments" field from a content type but for some reason it won't remove.

EDIT: Figured out how to delete/remove the Comments field from my content type.
First make it hidden (you can do this through the UI or in a script) then the above script can be used to remove it. Weird by it works

5 comments:

BRM013 said...
This comment has been removed by the author.
SaMolPP said...

hi
thxn for the post.
can you pls tell how to remove a summary links column's value [ within a group]
programmatically in a welcome page content type?
thnx!

BRM013 said...

Sorry its been a while...

You might be able to do it like I did above for the comments field.

If you are using Publishing then you could create your own page layout?

Or just hide it on the page using JavaScript?

SaMolPP said...

Thanks for the reply.Yes, I am using a custom page layout using VS 2012.But I am not able to
get the script for performing this.

BRM013 said...

If you are using a custom page layout, then you could just remove the fields from the page altogether.

A custom page layout allows you to control what page fields you want to display, the default layouts contain a few of the fields (summary links for instance) but if you don't want them, just delete them from the markup.

Am I understanding your problem? The script I mentioned is for removing the fields altogether, I think you are talking about just removing them from the page.