SharePoint: Limit folder content types. Change New Button Order

Scenario: A SharePoint document library or list containing various content types as seen in the screenshot below:

All Content Types

Suppose we want to show only two content types (HardwareItems and SanitaryItems) at the root of the site collection. This can be done by going into List Settings and clicking on Change new button order and default content type. Un-check the content types which are not needed as shown:

Change New Button Order

This is how same thing can be achieved through code:
using (SPSite site = new SPSite("http://aissp2013/sites/t1"))
{
    SPWeb web = site.RootWeb;
    SPList list = web.Lists["Hardware And Sanitary Products"];
    SPFolder folder = list.RootFolder;
    IList<SPContentType> uniqueContentTypeOrder = new List<SPContentType>();
    SPContentTypeCollection listContentTypes = list.ContentTypes;
    foreach (SPContentType ct in listContentTypes)
    {                   
        if (ct.Name == "SanitaryItems")
        {
            uniqueContentTypeOrder.Add(ct);
        }
        else if (ct.Name == "HardwareItems")
        {
            uniqueContentTypeOrder.Add(ct);
        }
    }

    if (uniqueContentTypeOrder.Count > 0)
    {
        folder.UniqueContentTypeOrder = uniqueContentTypeOrder;
    }
    list.Update();               
}
The main thing to note here is the folder is the root folder of the list. And this is the result.

Limited Content Types

Now suppose we want to limit folders of content type "HardwareItems" to show only content types "PowerTools" and "NutsAndBolts". This is how it can be done from UI. 
  • Create a folder of content type "HardwareItems" in the root folder of the list.
  • Then click on the folder created above and open the ECB menu. Click on "Change New Button Order".

Change New Button Order

  • Then select the two content types "PowerTools" and "NutsAndBolts" as shown:

PowerTools and NutsAndBolts

Finally once inside the folder of content type "HardwareItems", we will get to see only two content types:

PowerTools and NutsAndBolts

As can be seen this manual process needs to be followed every time a folder of content type "HardwareItems" is created. This can be automated through code. An "ItemAdded" event receiver can be added to the content type "HardwareItems" and then a code similar to what was written earlier can limit the folder types in "HardwareItems" folder to "PowerTools" and "NutsAndBolts".

profile for Nadeem Yousuf at SharePoint Stack Exchange, Q&A for SharePoint enthusiasts

+