Prash's Blog

Using SPFolder UniqueContentTypeOrder to Hide menu options July 15, 2009

Filed under: SharePoint — prazjain @ 5:41 pm
Tags: ,

I would explain the usage of this property by an example.
The example would be to hide a few items from the new menu button in menu bar.

I write a function that takes in a folder type and the list of content types that you want to hide from the

menu bar.

Now if you try to remove everything from the new menu button, you would get a runtime exception (just

like i got, so do not try that. Instead you might want to hide the new menu button!)

private void HideAllContentTypes(SPFolder rootFolder, IList<string> contentTypeNames)
 {
 IList<SPContentType> contentTypeCollection = rootFolder.ContentTypeOrder;

 List<SPContentType> contentTypes = new List<SPContentType>();

 foreach (SPContentType contentType in contentTypeCollection)
 {
 if (contentTypeNames.Contains(contentType.Name))
 continue;
 else
 contentTypes.Add(contentType);
 }

 rootFolder.UniqueContentTypeOrder = contentTypes;
 rootFolder.Update();
 }

Explanation of the code goes here :

1) IList<SPContentType> contentTypeCollection = rootFolder.ContentTypeOrder;
This step is important because you need to call SPFolder’s ContentTypeOrder property so that it initializes

internal storage structure and then when you later on call UniqueContentTypeOrder and set a collection on

it, you would not get a runtime exception.

2) Iterate through the SPContentType collection and create a new collection that does not include the

content types that you hate 🙂

3) Now you can assign SPFolder’s UniqueContentTypeOrder property with your new SPContentType

collection. Note you would get an runtime exception here if you tried to be smart and did not perform step 1.

4) Never forget to call Update() once you are done.

Advertisements
 

3 Responses to “Using SPFolder UniqueContentTypeOrder to Hide menu options”

  1. Julmas Says:

    To add to the confusion. I had trouble getting rid of the runtime Exception when setting the UniqueContentTypeOrder property. Here is my best explanation of how it works, although it is possible it is not the full story.

    First you have to check the IList returned by SPFolder.ContentTypeOrder.

    Now, if this propery is null, you can construct your own IList and assign it to the UniqueContentTypePropertyOrder.

    However, if the list returned is not null, you must work with the list you get back. That is, you Clear() it and then Add() new content types to it. You SHOULD NOT set UniqueContentTypeOrder or ContentTypeOrder in this case.

    In either of the cases above you Update() the folder when you are done.

    • Julmas Says:

      Please ignore my post above.

      The reason it didn’t throw an exception was because it didn’t actually do anything. I must set the UniqueContentTypeOrder property somehow, but sharepoint won’t let me 😦

      • prazjain Says:

        It would throw a runtime exception if you are trying to assign an empty collection. Could you check if your collection has some values (atleast one) before assigning it to UniqueContentTypeOrder

        Cheers


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s