Forum Discussion

Darren Parkinson's avatar
Darren Parkinson
Brass Contributor
Sep 26, 2017

Can't turn off "Visible on New Button" for Content Type using PnP PowerShell

Hi,

 

I'm having trouble turning off the "Visible on New Button" option for a Content Type on a Document Library.  I'm trying to use the "UniqueContentTypeOrder" to do that.  

 

So far, it's not taking -- any thoughts?  Snippet of script below.

Many thanks.

 

$doclib = Get-PnPList -Identity "Quotes" -Includes RootFolder.UniqueContentTypeOrder
$lct = $doclib.ContentTypes
$ctx.Load($lct)
$ctx.ExecuteQuery()
$rootFolder = $doclib.RootFolder
$lucto = $doclib.ContentTypes | ?{$_.Name -ne "<Content Type I'm Trying to Hide>"}
$list = New-Object 'System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]'
foreach ($i in $lucto) {
    $id = $i.Id
    $list.Add($id)
}
$rootFolder.UniqueContentTypeOrder = $list
$rootFolder.Update()
$doclib.Update()
  • Shouldn't you have 

    $ctx.ExecuteQuery()

    at the end? 

     

    Haven't done this in powershell but the used method is the right one. 

  • Thanks. Yeah, I tried that, but it errors (as is very common!) :( Not having much fun with SharePoint today
  • nfsg20's avatar
    nfsg20
    Copper Contributor

    Darren Parkinson Try Invoke-PnPQuery, you dont need to use ctx at all.

     

    Working Code for me (I'm using the opposite approach, telling the function what content type I want to have in the "New" menu):

     

    function SetVisibleContentType($listName, $ctName) {
        $ct = Get-PnPContentType -List $listName -Identity $ctName
    
        $list = Get-PnPList -Identity $listName -Includes RootFolder.UniqueContentTypeOrder
    
        $idList = New-Object "System.Collections.Generic.List[Microsoft.SharePoint.Client.ContentTypeId]"
        $idList.Add($ct.Id)
    
        $list.RootFolder.UniqueContentTypeOrder = $idList
        $list.RootFolder.Update()
    
        Invoke-PnPQuery
    }

     

    If you want to show more than one content type, just pass a string-array of content type names, or change the logic to show all but the provided conten type name.