0

I am trying to delete the remaining content type from the new button in my library. I am getting this error constantly which is weird since I am able to make the changes through the UI. Any help would be appreciated.

        public async Task HideAllContentTypesInNewButtonAsync(ClientContext clientContext, string listTitle, params string[] excludedContentTypeNames)
    {
        List list = clientContext.Web.GetListByTitle(listTitle);
        await list.EnsurePropertiesAsync(toLoad => toLoad.ContentTypes, toLoad => toLoad.ContentTypes.Include(ct => ct.Name));

        var contentTypesToHide = list.ContentTypes
                                     .Where(contentType => excludedContentTypeNames.All(name => contentType.Name != name))
                                     .ToArray();

        HideContentTypesInNewButton(list, contentTypesToHide);
        list.Update();
        log.LogDebug($"Hiding Content Types on list '{listTitle}'.");

        await clientContext.ExecuteQueryRetryAsync();
    }

enter image description here enter image description here

3
  • Why would you want to do this? Jan 17 at 10:12
  • We don't create new files we only upload files so the functionality is useless for us. Jan 17 at 10:43
  • I've added an answer which explains how to hide the "New" button. Jan 17 at 12:11

2 Answers 2

1

If you do not want to display the "New" button, it can be hidden using JSON formatting.

Navigate to the list or document library >> Click on the View dropdown and choose “Format current view” >> Click on “Advanced Mode”

Paste the JSON below to hide the New button from the toolbar.

{
  "commandBarProps" : {
    "commands": [
      {
        "key": "new",
        "hide": true
      }   
    ]
  }
}

enter image description here

Hide a Button in SharePoint Online List or document library Toolbar

2
  • Thanks for the input but I would like to keep this as a last resort. The only problem with this approach is that I will have to manually do it for each environment. Jan 17 at 12:25
  • I also recommend you hide the "New" button using JSON formatting. In addition, you can use CSS code to hide the "New" button. For reference: learn.microsoft.com/en-us/answers/questions/545423/…
    – Zella_msft
    Jan 18 at 7:17
1

I've had the same issue but with PowerShell and solved this by removing all content types in the order and then creating a new list.

In this case, i pass the name of the content types in the parameter $order = "ContentTypeA", "ContentTypeB"

# Sets the content type order on list
Function Set-ContentTypeOrderOnList
{
    Param
    (
        [parameter(mandatory=$true)]
        $webUrl,
        [parameter(mandatory=$true)]
        $order,
        [parameter(mandatory=$true)]
        $rootFolder
    )

    $web = Get-SPWeb $webUrl
    $list = $web.Lists | ? { $_.RootFolder.Name -eq $rootFolder }

    if($list.ContentTypes[$order[0]] -ne $null)
    {
        $currentListOrder = $list.RootFolder.ContentTypeOrder

        for($i = $currentListOrder.Count; $i -gt 0; $i--)
        {
            $lct = $currentListOrder[$i]
            $disableoutput = $currentListOrder.Remove($lct)
        }

        $currentListOrder.Add($list.ContentTypes[$order[0]])

        $disableoutput = $currentListOrder.Remove($currentListOrder[0])

        foreach($ctToAdd in $order | where { $_ -ne $order[0]})
        {
            $currentListOrder.Add($list.ContentTypes[$ctToAdd])
        }

        $list.RootFolder.UniqueContentTypeOrder = $currentListOrder
        $list.RootFolder.Update()
    }

    $web.Dispose()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.