locked
Adding New Custom Folder Content Type doesn't show up on the Document Library New Menu RRS feed

  • Question

  • I been trying to add new custom folder content type to the document library programatically in my sandbox environment and it does show up on the document library. But, when I move same code to the almost similar configured SP2010 environment, adding new folder content type to the document library doesn't show up on the document library's new menu.

    Here is the code? Since this code works fine in my sandbox environment, is there any other settings I should check on the server environment which would cause hiding content type on new menu of document library.

      class Program
      {
        static void Main(string[] args)
        {
          SPSite spSite = new SPSite("http://sp2010vm");
          foreach (SPWeb spWeb in spSite.AllWebs)
          {
            spWeb.AllowUnsafeUpdates = true;
    
            //For each site, cycle through lists
            bool isDocLibraryExists = false;
    	      foreach(SPList spList in spWeb.Lists)
    	      {
              if(spList.Title == "Classified Documents")
    		      {
                 isDocLibraryExists = true; 
              }
            }
          
            if(isDocLibraryExists == true)
            {
              //Create new doc library
              SPList splist = spWeb.Lists["Classified Documents"];
    
              //Create Content Type
              SPContentType documentContentType = spWeb.AvailableContentTypes["Classified Documents Folder"]; 
              bool isContentTypeExists = false;
              foreach (SPContentType spContentType in spWeb.ContentTypes)
              {
                 if(spContentType.Name == "Classified Documents Folder")
      		       {
                  isContentTypeExists = true;         
                 }        
              }
              if(isContentTypeExists == false)
      	      {
                splist.ContentTypes.Add(documentContentType);
                splist.Update();
              }
    
              spWeb.Dispose();          
            }
    
            spSite.Dispose();
          }
        }
      }
    Monday, October 18, 2010 8:59 PM

Answers

  • Ok.. Guys.. I have the answer from above thread. The actual code doesn't work but there is a reference of the blog link and code which works with little twicks..

    http://shahzadtariq.blogspot.com/2009/09/custom-content-type-visible-on-new.html

    Here is my complete code which works fine... Blog has explanation of why it behaves differently on some document libraries it shows up on new menu and some others not...

      class Program
      {
        static void Main(string[] args)
        {
          SPSite spSite = new SPSite(http://sp2010vm);
          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[] ordContentTypes = new SPContentType[spList.ContentTypes.Count - 1];
                    int ordContentTypesCount = 0;
                    int iContentTypeCount = spList.ContentTypes.Count;
    
                    for (int iCount = 0; iCount < iContentTypeCount; iCount++)
                    {
                      if (spList.ContentTypes[iCount].Name != "Folder")
                      {
                        ordContentTypes[ordContentTypesCount] = spList.ContentTypes[iCount];
                        ordContentTypesCount++;
                      }
                    } 
    
                    //set the content types for the "new" drop down list
                    spList.RootFolder.UniqueContentTypeOrder = ordContentTypes;
                    spList.RootFolder.Update();
                  }
                }
              }
            }
            spWeb.AllowUnsafeUpdates = false;
            spWeb.Dispose();          
          }
    
          spSite.Dispose();
        }
      }
    • Marked as answer by Nik Patel Tuesday, October 19, 2010 5:43 AM
    Tuesday, October 19, 2010 5:32 AM

All replies

  • Hi Nik,

    what you need to do is to setup a custom New / Edit / Display Form for your Content Type because basically Folder doesn't have such forms. When you inherate from folder you won't get those forms neither.

    After you had set up your new forms you need to assign them to your content type.

    http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype_properties.aspx

    Take a look at the object model there are EditFormUrl, NewFormUrl, DispFormUrl. You although must create your own application page for your special folder creation and Stuff like that

    Regards Stefan.


    http://www.n8d.at/blog
    MCTS - SharePoint / WSS Configuration and Development
    Monday, October 18, 2010 10:53 PM
  • Thanks Stefan. Interesting notes from you and logically makes sense but I haven't created any of these forms or pages on my sandbox environment and I don't have any issues with that.. In my sandbox environment, I can add the custom folder content type and it shows up on my document library's new menu.

    Thanks anyways. Will keep looking for answers.

    Nik

    Tuesday, October 19, 2010 4:16 AM
  • Tuesday, October 19, 2010 4:29 AM
  • Ok.. Guys.. I have the answer from above thread. The actual code doesn't work but there is a reference of the blog link and code which works with little twicks..

    http://shahzadtariq.blogspot.com/2009/09/custom-content-type-visible-on-new.html

    Here is my complete code which works fine... Blog has explanation of why it behaves differently on some document libraries it shows up on new menu and some others not...

      class Program
      {
        static void Main(string[] args)
        {
          SPSite spSite = new SPSite(http://sp2010vm);
          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[] ordContentTypes = new SPContentType[spList.ContentTypes.Count - 1];
                    int ordContentTypesCount = 0;
                    int iContentTypeCount = spList.ContentTypes.Count;
    
                    for (int iCount = 0; iCount < iContentTypeCount; iCount++)
                    {
                      if (spList.ContentTypes[iCount].Name != "Folder")
                      {
                        ordContentTypes[ordContentTypesCount] = spList.ContentTypes[iCount];
                        ordContentTypesCount++;
                      }
                    } 
    
                    //set the content types for the "new" drop down list
                    spList.RootFolder.UniqueContentTypeOrder = ordContentTypes;
                    spList.RootFolder.Update();
                  }
                }
              }
            }
            spWeb.AllowUnsafeUpdates = false;
            spWeb.Dispose();          
          }
    
          spSite.Dispose();
        }
      }
    • Marked as answer by Nik Patel Tuesday, October 19, 2010 5:43 AM
    Tuesday, October 19, 2010 5:32 AM