Thursday, September 17, 2009

Custom Content Type, Visible on New Button

I was adding a custom content type to a form library and I was using the same code mentioned in the following link:
http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/13333670-d1ea-41ec-8ebc-4ad9ecb04927

But my custom content type was not set as "Visible on New Button", after that I created a new form library and tried to run the same code which I was using before and it worked for newly created form library.

I started comparing the difference between 2 form libraries because same code was working for one form library but not working for other one, after digging into further I realized whenever you change the order of content type through UI while using the "Change new button order and default content type" option under the settings of form library, code does not work.

So consequently I can say in order to code get working don't change the form library content type order through UI, there can be any other reason for which code was not working but I could find only this reason, it may help someone in future.

7 comments:

  1. SPContentType contentTypeAgain = spList.ContentTypes.Add(parentWeb.AvailableContentTypes[contentType]);
    spList.Update();
    spList.RootFolder.UniqueContentTypeOrder = null;
    spList.RootFolder.Update();

    //get a list of content types for the "new" drop down on the list
    SPContentType[] orderedContentTypes = new SPContentType[spList.ContentTypes.Count];
    int i = 0;
    foreach (SPContentType listContentType in spList.ContentTypes)
    {
    orderedContentTypes[i] = listContentType;
    i++;
    //Debug.Instance.WriteToTraceLog(i.ToString());
    }
    //set the content types for the "new" drop down list
    spList.RootFolder.UniqueContentTypeOrder = orderedContentTypes;
    spList.RootFolder.Update();

    ReplyDelete
  2. Hi Tabi,

    So the code which you are using, will work even someone has changed the content type through UI in SharePoint?

    -Shahzad

    ReplyDelete
  3. Thanks Tabi for putting that code in.

    ReplyDelete
  4. What if you have Content Types in there you don't want to be visible. The current solution seems to make all the content types visible.

    ReplyDelete
  5. you can use SPContentType.Hidden property if you don't want to show a certain content type.

    ReplyDelete
  6. Hope it helps.

    IList orderedContentTypes = list.RootFolder.UniqueContentTypeOrder;
    if (!orderedContentTypes.Contains(contentTypeAgain))
    {
    orderedContentTypes.Add(contentTypeAgain);
    }
    list.RootFolder.UniqueContentTypeOrder = orderedContentTypes;
    list.RootFolder.Update();

    ReplyDelete