Adding Content Type to New Dropdown

by bronyx85 at 2013-02-28 06:10:34

Hi, i’ve added a content type to a do library using powershell (this document library already has 20+ content types), but when I add my new custom content type, for some reason it does not appear on the New Dropdown, this means I have to manually go into each document library and change the settings to ensure my new content type is selectable (very cumbersome if you have 5,000 + library’s)

How can I achieve by powershell, to make a content type visible?

Thanks in advance.
by AlexBrassington at 2013-02-28 08:53:20
Can you post up the script you ran? Also does the content type appear on the settings page?
by bronyx85 at 2013-03-01 03:25:47
yes, the content type appears in the library settings page.

The issue is Alex, when a content type is added to a document library, the newly added content type inherits its "Visible" attribute from the last added content type. 7

E.g if we have a content type in the library called for e.g Content Type A and its visibility is set to Show, and we have another Content Type called Content Type B and its Visibility to hide from the new dropdown, then any new Content Types added in will always look at the last Content Type and inherit the last content types visibility setting, in my example, Content Type B.

Hope that makes sense.

Rgds
by bronyx85 at 2013-03-01 09:08:25
Hi,
I Have used Phil Childs Script here :
#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://portal
$lookForList = "Shared Documents"

#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {

write-host "Checking site:"$.Title

#Make sure content types are allowed on the list specified
$docLibrary = $
.Lists[$lookForList]

if ($docLibrary -ne $null)
{
$docLibrary.ContentTypesEnabled = $true
$docLibrary.Update()

#Add site content types to the list
$ctToAdd = $site.RootWeb.ContentTypes["Sales Document"]
$ct = $docLibrary.ContentTypes.Add($ctToAdd)
write-host "Content type" $ct.Name "added to list" $docLibrary.Title
$ctToAdd = $site.RootWeb.ContentTypes["IT Document"]
$ct = $docLibrary.ContentTypes.Add($ctToAdd)
write-host "Content type" $ct.Name "added to list" $docLibrary.Title
$docLibrary.Update()
}
else
{
write-host "The list" $lookForList "does not exist in site" $.Title
}
}
#Dispose of the site object
$site.Dispose()


How can I incorporate the UniqueContentTypeOrder cmdlet in here.

From my understanding, the UniqueContentTypeOrder cmdlet allows all the content types to appear in any order, but be visible…

I have tried to add it before and after the doclibrary.update() command, but I keep getting an error about the property does not exist.

Please can you assist on how to get this cmdlet to work in the above script.

Rgds
by AlexBrassington at 2013-03-04 11:41:05
Either the moderation process has jinxed me again or i lost signal posting my reply.

I’ll try again, this is a fairly tricky thing you ask. Here’s some code that works, i’m going to polish it up into a something a bit more practical:

#May or may not be required. Probably not tbh
[void][reflection.assembly]::loadwithpartialname("System.Collections")

#Get site object and specify name of the library to look for in each site
$site = Get-SPSite http://portal
$lookForList = "Shared Documents"

#Walk through each site and change content types on the list specified
$site | Get-SPWeb -Limit all | ForEach-Object {

write-host "Checking site:"$
.Title

#Make sure content types are allowed on the list specified
$docLibrary = $.Lists[$lookForList]

if ($docLibrary -ne $null)
{
$docLibrary.ContentTypesEnabled = $true
$docLibrary.Update()

#Add site content types to the list
$ctToAdd = $site.RootWeb.ContentTypes["Sales Document"]
$ct = $docLibrary.ContentTypes.Add($ctToAdd)
write-host "Content type" $ct.Name "added to list" $docLibrary.Title

$docLibrary.Update()
#Get the root folder - this is where content types are stored
$rootFolder = $docLibrary.RootFolder

#Get the content types already in the list
$contentTypesInPlace = [Microsoft.SharePoint.SPContentType[]] $rootFolder.UniqueContentTypeOrder
#Add our content type. Note that this doesn't check for duplicates
$contentTypesInPlace = $contentTypesInPlace + $ctToAdd
#Set the uniquecontenttypeorder to the collection we made above
$rootFolder.UniqueContentTypeOrder = [Microsoft.SharePoint.SPContentType[]] $contentTypesInPlace
#Update the root folder
$rootFolder.Update()

Write-Host "Content Type added to the new item creation options"
}
else
{
write-host "The list" $lookForList "does not exist in site" $
.Title
}
}
#Dispose of the site object
$site.Dispose()
by bronyx85 at 2013-03-05 03:04:01
Hi Alex,

This code doesn’t seem to work propely, it adds the content type to the library, but it does set the Unique Order.
It throws back in Error "Method invocation failed because [System.RuntimeType] doesn’t contain a method named op_Addition.

Any ideas?
by AlexBrassington at 2013-03-05 09:51:55
Yup. That code wasn’t really production ready.

Try this:
Function Is-ContentTypeInNewButton {
<#
.SYNOPSIS
Checks to see if a content type is present in a list's new button
.DESCRIPTION
Checks for the presence of a particular content type in the new button on a list.
.PARAMETER ContentTypeName
The Name of the content type to check
.PARAMETER SPList
An SPList item to check.
.EXAMPLE
$Site = Get-SPSite "http://sharepoint/sites/site&quot;
$List = $site.rootWeb.Lists["Test_List"]
Is-ContentTypeInNewButton -ContentTypeName "Email" -SPList $List
This will return a $true if the content type 'Email' is present in the new button options.
#>
[CmdletBinding()]
Param ([parameter(Mandatory=$true)][string] $ContentTypeName,
[parameter(Mandatory=$true)][Microsoft.SharePoint.SPList] $SPList)
BEGIN { Write-Verbose "Begining Is-ContentTypeInNewButton" }
PROCESS{
#get the uniquecontenttypes from the list root folder
$rootFolder = $SPList.RootFolder
$contentTypesInPlace = [Microsoft.SharePoint.SPContentType] $rootFolder.UniqueContentTypeOrder

#Check if any of them are the same as the test content type
$results = $contentTypesInPlace | where { $_.Name -eq $ContentTypeName}
if ($results -ne $null)
{
Write-Verbose "$ContentTypeName Found"
return $true
}
else
{
Write-Verbose "$ContentTypeName Not Found"
return $false
}
}

END { Write-Verbose "Exiting Is-ContentTypeInNewButton" }
}

Function Ensure-ContentTypeInList{
<#
.SYNOPSIS
Checks to see if a content type is added to a list and adds it if not present
.DESCRIPTION
Checks for the presence of a particular content type in a list and if it is not found then adds it. This will also update the Library to allow content types if required.
.PARAMETER ContentTypeName
The Name of the content type to add
.PARAMETER SPList
An SPList item upon which the content type should be checked and added if needed.
.EXAMPLE
$Site = Get-SPSite "http://sharepoint/sites/site&quot;
$List = $site.rootWeb.Lists["Test_List"]
Ensure-ContentTypeInList -ContentTypeName "Email" -SPList $List
This will ensure that the content type 'Email' is present in the list 'Test_list'.
#>
[CmdletBinding()]
Param ( [parameter(Mandatory=$true,ValueFromPipeline=$true)][string] $ContentTypeName,
[parameter(Mandatory=$true)][Microsoft.SharePoint.SPList] $SPList)

BEGIN { Write-Verbose "Begining Ensure-ContentTypeInList" }
PROCESS {

#Check to see if the content type is already in the list
$contentType = $SPList.ContentTypes[$ContentTypeName]
if ($ContentType -ne $null)
{
#Content type already present
Write-Verbose "$ContentTypeName already present in list"
Return $true
}
else
{
Write-Verbose "$ContentTypeName not in list. Attempting to add"
if (!$SPList.ContentTypesEnabled)
{
Write-Verbose "Content Types disabled in list $SPList, Enabling"
$SPList.ContentTypesEnabled = $true
$SPList.Update()
}
#Add site content types to the list from the site collection root
$ctToAdd = $SPList.ParentWeb.Site.RootWeb.ContentTypes[$ContentTypeName]
if($ctToAdd -eq $null)
{
Write-Error "Error - Content Type could not be found in the Site Collection"
#I don't believe this will be called.
return $false
}
$SPList.ContentTypes.Add($ctToAdd) | Out-Null
$SPList.Update()
Write-Verbose "$ContentTypeName added to list"
return $true
}
}
END {
Write-Verbose "Exiting Ensure-ContentTypeInList"
}
}

Function Ensure-ContentTypeInNewButton{
<#
.SYNOPSIS
Checks to see if a content type is availible in the new button on a list and attempts to add it if not present
.DESCRIPTION
Adds a content type to the UniqueContentTypeOrder list on the root folder of a document library. Thus adding it to the new button for that list This will also add the content type to the list if it is not already added.
.PARAMETER ContentTypeName
The Name of the content type to add
.PARAMETER SPList
An SPList item which should be checked and to which the content type should be added if needed.
.EXAMPLE
$Site = Get-SPSite "http://sharepoint/sites/site&quot;
$List = $site.rootWeb.Lists["Test_List"]
Ensure-ContentTypeInNewButton -ContentTypeName "Email" -SPList $List
This will ensure that the content type 'Email' is present in the new button on the list 'Test_list'.
#>
[CmdletBinding()]
Param ( [parameter(Mandatory=$true,ValueFromPipeline=$true)][string] $ContentTypeName,
[parameter(Mandatory=$true)][Microsoft.SharePoint.SPList] $SPList)
BEGIN {
Write-Verbose "Begining Ensure-ContentTypeInNewButton"
#get the uniquecontenttypes from the list root folder
$contentTypesInPlace = New-Object 'System.Collections.Generic.List[Microsoft.SharePoint.SPContentType]'
$contentTypesInPlace = $SPList.RootFolder.UniqueContentTypeOrder
$dirtyFlag = $false
}
PROCESS {

#Check the content type isn't already present in the content type
$AlreadyPresent = Is-ContentTypeInNewButton -ContentTypeName $ContentTypeName -SPList $SPList
if ($AlreadyPresent)
{
Write-Verbose "$ContentTypeName is already present in the new button"
}
else
{
#Check that there really is such a content type
$ContentTypePresent = Ensure-ContentTypeInList $ContentTypeName $SPList
#Catch error events
if ($ContentTypePresent)
{
#We now know that the content type is not in the new button and is present in the list. Carry on adding the content type

$ctToAdd = $SPList.ContentTypes[$ContentTypeName]

#add our content type to the unique content type list
$contentTypesInPlace = $contentTypesInPlace + $ctToAdd
$dirtyFlag = $true
Write-Verbose "$ContentTypeName queued to add to the new button"
}
else
{
Write-Error -Message "Content type could not be added to the list."
}
}
}
End{
#Set the UniqueContentTypeOrder to the collection we made above
if ($dirtyFlag)
{
$SPList = $SPList.ParentWeb.Lists[$SPList.ID]
$rootFolder = $SPList.RootFolder
$rootFolder.UniqueContentTypeOrder = [Microsoft.SharePoint.SPContentType] $contentTypesInPlace

#Update the root folder
$rootFolder.Update()
Write-Verbose "ContentType(s) added to the new button in list $($SPList.Name)"
}
else
{
Write-Verbose "No changes"
}
Write-Verbose "Exiting Ensure-ContentTypeInNewButton"

}
}


I’ve blogged about it here with some information on how to use it and some other supporting stuff. Critique of PowerShell welcomed.
http://alexbrassington.wordpress.com/ad … owershell/
by bronyx85 at 2013-03-08 03:24:21
Thank you much Alex.
your a star