Adding New Custom Content Type doesn’t show up on the Document Library New Menu

As usual, every other month, I come across bizarre SharePoint issue which forces me to write a detailed blog. This time it’s related to making newly added custom content type available on the document library’s new menu. The default OOB behavior while adding a new content type to the document library is to set the hidden properly of the content type to false. Hidden property controls whether content type will be available on the list’s or document library’s new menu or not. In other words, adding new content type to the document library should show the content type on the new menu as a default OOB behavior.

Armed with this basic knowledge, I have tried to add new custom folder content type to the document library programmatically in my sandbox environment with following code and as expected, new custom folder content type does show up on the document library.

 


    static void Main(string[] args)
        {
            SPSite spSite = new SPSite("<a href="http://sp2010vm">http://sp2010vm</a>");
            foreach (SPWeb spWeb in spSite.AllWebs)
            {
                spWeb.AllowUnsafeUpdates = true;

                SPListCollection lists = spWeb.Lists;
                for (int i = 0; i < lists.Count; i++)
                {
                    if (lists[i] is SPDocumentLibrary)
                    {
                        if (((SPDocumentLibrary)lists[i]).IsCatalog == false)
                        {
                            SPDocumentLibrary spList = (SPDocumentLibrary)lists[i];
                            if (spList.Title == "Classified Documents")
                            {
                                //Enable Content Types Allowed and Disable the Folder Creation
                                spList.EnableFolderCreation = false;
                                spList.ContentTypesEnabled = true;
                                spList.Update();

                                //Create Content Type
                                if (spList.ContentTypes["Classified Documents Folder"] == null)
                                {
                                    spList.ContentTypes.Add(spWeb.AvailableContentTypes["Classified Documents Folder"]);
                                    spList.Update();
                                }
                            }
                        }
                    }
                }
                spWeb.AllowUnsafeUpdates = false;
                spWeb.Dispose();                  
            }

            spSite.Dispose();
        }

Things got out of the hand when I moved the same code to the almost similarly configured, previously deployed SP2010 staging environment. In the staging environment, adding new folder content type programmatically to the document library doesn’t show up the newly added content type on the document library’s new menu. After trying several different options, I have compared all the site, site collection, web application, and farm features and everything seems virtually fine. After raising several questions on the Microsoft forum, finally I had stumbled upon the wonderful blog from Shahzad and his explanation and one of the resolutions in his comment section provided me enough fuel to fix this issue. This resolution is one of the most bizarre situations I have come across in the SharePoint.

As it turned out, while testing new custom content type in my sandbox environment, above code was running fine on the new document libraries. One of the key features of these sandbox document libraries was content types settings weren’t changed through UI. On the other hand, all the document libraries content type visibility settings were changed through UI in the staging environment. If you change the document library content type’s new menu visibility and order settings through UI using the “Change new button order and default content type” link, above code doesn’t work and ignores adding the new content type on the new menu.

The resolution to fix this issue is to update the content type order and visibility settings after content types are added to the document library programmatically as the code shown below. This will ensure that newly added custom content type new menu visibility settings are set properly with/without whether content type settings were updated manually through UI on the document library or not.


        static void Main(string[] args)
        {
            SPSite spSite = new SPSite("<a href="http://sp2010vm">http://sp2010vm</a>");
            foreach (SPWeb spWeb in spSite.AllWebs)
            {
                spWeb.AllowUnsafeUpdates = true;

                SPListCollection lists = spWeb.Lists;
                for (int i = 0; i < lists.Count; i++)
                {
                    if (lists[i] is SPDocumentLibrary)
                    {
                        if (((SPDocumentLibrary)lists[i]).IsCatalog == false)
                        {
                            SPDocumentLibrary spList = (SPDocumentLibrary)lists[i];
                            if (spList.Title == "Classified Documents")
                            {
                                //Update the list properties
                                spList.EnableFolderCreation = false;
                                spList.ContentTypesEnabled = true;
                                spList.Update();

                                //Add Content Type
                                if (spList.ContentTypes["Classified Documents Folder"] == null)
                                {
                                    spList.ContentTypes.Add(spWeb.AvailableContentTypes["Classified Documents Folder"]);
                                    spList.Update();
                                    spList.RootFolder.UniqueContentTypeOrder = null;
                                    spList.RootFolder.Update();
                                }

                                //Get a list of content types for the "new" drop down on the list
                                SPContentType[] arrContentTypes = new SPContentType[spList.ContentTypes.Count - 1];
                                int arrContentTypesCount = 0;
                                int iContentTypeCount = spList.ContentTypes.Count;

                                for (int iCount = 0; iCount < iContentTypeCount; iCount++)
                                {
                                    if (spList.ContentTypes[iCount].Name != "Folder")
                                    {
                                        arrContentTypes[arrContentTypesCount] = spList.ContentTypes[iCount];
                                        arrContentTypesCount++;
                                    }
                                }

                                //Set the content types for the "new" drop down list
                                spList.RootFolder.UniqueContentTypeOrder = arrContentTypes;
                                spList.RootFolder.Update();
                            }
                        }
                    }
                }

                spWeb.AllowUnsafeUpdates = false;
                spWeb.Dispose();                  
            }

            spSite.Dispose();
        }

Here are some of the MS Forum references for more details.

Hope it helps.

Advertisements
This entry was posted in SP2010 DEV General. Bookmark the permalink.