locked
Adding a content type - not Visible on New Button RRS feed

  • Question

  • Hi,

    I'm associating a content type to a document library, programmatically, but I dont get  "Visible on New Button" property checked.

    I'm trying to add e.g. 'Picture' content type in all document libraries of a site:
    Here is some code:

    using (SPSite site = new SPSite(url))
    {
             using (SPWeb web = site.OpenWeb())
            {
                    web.AllowUnsafeUpdates = true;
                    SPListCollection lists = web.Lists;

                    for (int i=0; i<lists.Count; i++)
                    {
                         if(lists[i] is DocumentLibrary)
                        {
                            SPDocumentLibrary olist = (SPDocumentLibrary)lists[i];
                            olist.ContentTypes.Add(web.ContentTypes["Picture"]);
                            olist.Update();   
                        }
                    }
            }

    }



    Thanx in advance

     
    Thursday, March 19, 2009 10:38 AM

Answers

  • Hi,

    This works for me.

    using (SPSite site = new SPSite(this.textBox1.Text))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPListCollection lists = web.Lists;

                        for (int i = 0; i < lists.Count; i++)
                        {
                            if (lists[i] is SPDocumentLibrary)
                            {
                                if (((SPDocumentLibrary)lists[i]).IsCatalog == false)
                                {
                                    SPDocumentLibrary olist = (SPDocumentLibrary)lists[i];
                                    if (olist.ContentTypesEnabled == false)
                                    {
                                        olist.ContentTypesEnabled = true;
                                        olist.Update();
                                    }

                                    if (olist.ContentTypes["Picture"] == null)
                                    {
                                        olist.ContentTypes.Add(web.ContentTypes["Picture"]);
                                        olist.Update();
                                    }
                                }
                            }
                        }
                        web.AllowUnsafeUpdates = false;
                    }

    I think the difference is that I set the SPList.ContentTypesEnabled property and then update the list before adding the content type (please see section in bold).  I also check to see if the list isnt a catalog list (not masterpage and pagelayouts etc).

    Hope this helps

    Dave


    My SharePoint Blog - www.davehunter.co.uk/blog
    Thursday, March 19, 2009 2:28 PM

All replies

  • Hi,

    You will need to enable content types for the document library like this:


    SPDocumentLibrary olist = (SPDocumentLibrary)lists[i];
    olist.ContentTypesEnabled = true;
    olist.ContentTypes.Add(web.ContentTypes["Picture"]);
    olist.Update();   

     

    Thursday, March 19, 2009 12:32 PM
  • Thanx for your reply. Tried it. But it didn't help.

    I get the content type added correctly. The problem is that I dont get the "Visible on new Button" property enabled.

    Thursday, March 19, 2009 12:41 PM
  • Strange, try to change default content type of the list:
    http://hristopavlov.wordpress.com/2008/08/07/changing-the-default-list-content-type-programatically/

    Maybe this will help.

    Cheers,
       Jakub G
    Thursday, March 19, 2009 2:15 PM
  • Hi,

    This works for me.

    using (SPSite site = new SPSite(this.textBox1.Text))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        web.AllowUnsafeUpdates = true;
                        SPListCollection lists = web.Lists;

                        for (int i = 0; i < lists.Count; i++)
                        {
                            if (lists[i] is SPDocumentLibrary)
                            {
                                if (((SPDocumentLibrary)lists[i]).IsCatalog == false)
                                {
                                    SPDocumentLibrary olist = (SPDocumentLibrary)lists[i];
                                    if (olist.ContentTypesEnabled == false)
                                    {
                                        olist.ContentTypesEnabled = true;
                                        olist.Update();
                                    }

                                    if (olist.ContentTypes["Picture"] == null)
                                    {
                                        olist.ContentTypes.Add(web.ContentTypes["Picture"]);
                                        olist.Update();
                                    }
                                }
                            }
                        }
                        web.AllowUnsafeUpdates = false;
                    }

    I think the difference is that I set the SPList.ContentTypesEnabled property and then update the list before adding the content type (please see section in bold).  I also check to see if the list isnt a catalog list (not masterpage and pagelayouts etc).

    Hope this helps

    Dave


    My SharePoint Blog - www.davehunter.co.uk/blog
    Thursday, March 19, 2009 2:28 PM
  • Tried it. Didn't work.

    One additional thing I'd like to mention is that the 'Picture' content type source is home site (http://myserver).
    It applies just fine (with Visible on New Button set) in the document libraries that are created on the home site.

    The problem(not Visible on New button not set) occurs for the document libraries that belong to the subsites. (e.g  http://mysite/subsite)


    Any idea?
    • Proposed as answer by Shahzad Tariq Thursday, September 17, 2009 7:06 PM
    Friday, March 20, 2009 10:02 AM
  • Can you check on your document library how many contenttypes are shown -


    yourDocLibrary.RootFolder.UniqueContentTypeOrder will give you count of contenttypes available under New option.

    Suhaib

    • Proposed as answer by Nico Martens Wednesday, November 23, 2011 9:53 AM
    • Unproposed as answer by Nico Martens Wednesday, November 23, 2011 9:53 AM
    Friday, March 20, 2009 8:38 PM
  • Hi there,

    I don't know if you are still looking for the answer or not but I was having the same issue, and the aforementioned code was not working for me either but after digging more into this issue I found a solution, please check the following link:

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

    Hoping it will solve your problem.

    -Shahzad
    • Proposed as answer by Mohamed Hachem Thursday, February 25, 2010 9:51 PM
    Thursday, September 17, 2009 7:08 PM
  • Hello

    I had same problem adding content type from root web to page library of given sub site

    I have been inspired by the work of Shahzad

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

    here is the code

     

         SPContentType contentTypeAgain = pagesList.ParentWeb.Site.RootWeb.ContentTypes[myContentTypeID];

         pagesList.Update();

         pagesList.RootFolder.UniqueContentTypeOrder = null;

         pagesList.RootFolder.Update();

     

          //get a list of content types for the "new" drop down on the list

          SPContentType[] orderedContentTypes = new SPContentType[pagesList.ContentTypes.Count];

          int i = 0;

          foreach (SPContentType listContentType in pagesList.ContentType
         {

                                        orderedContentTypes[i] = listContentType;

                                        i++;

         }

         //set the content types for the "new" drop down list

          pagesList.RootFolder.UniqueContentTypeOrder = orderedContentTypes;

          pagesList.RootFolder.Update();

     

    Cheers


    Momo
    • Proposed as answer by Mohamed Hachem Thursday, February 25, 2010 11:00 PM
    Thursday, February 25, 2010 11:00 PM