Document libraries, Folders & Content Types

I have been asked a few times if it is possible to associate different Content Types with different folders within the same document library.  From the Web UI in WSS this does not seem to be possible.  However,  there are sneeky ways to achieve this.

Scenario:  I have a document library that has some document content types (say Report, Review & Results).  I want to have people create Reports in the root of my document library & only Review and Results in sub folders.

Solution

First up I would create a new folder Content Type (called say Collateral Folder) and associate that with the document library.  Then I would make sure all my document Content Types are associated with the document library but set only Report to show on the new button (you do all this in the content type settings area of your library) .. nothing special yet.

At this point you need to know how to write a document library event handler (If you want to know how to do this I would read this blog post: https://blogs.msdn.com/brianwilson/archive/2007/03/05/part-1-event-handlers-everything-you-need-to-know-about-microsoft-office-sharepoint-portal-server-moss-event-handlers.aspx)

What you need to do is create an event handler for this library that uses the ItemAdded event to set the UniqueContentTypeOrder property on the folder.  This will set the correct items on the New button for you.

(Caveat:  I have pulled this from an example & I have and chopped out other code for the purposes of this post ... the following may or may not compile ... its just an example :)

public override void ItemAdded(SPItemEventProperties properties)
{

base.ItemUpdated(properties);

SPListItem item = properties.ListItem;

// get the content types we need... 
SPContentType collateralFolderCT= item.ParentList.ContentTypes["Collateral Folder"];
SPContentType reviewCT= item.ParentList.ContentTypes["Review"];
SPContentType resultsCT= item.ParentList.ContentTypes["Results"];

// if the item that was created is a Collateral Folder then we want to set the property.
if(item.ContentType.Id == collateralFolderCT.Id)
{

// build a collection of content types that we want on the New button
Collection<SPContentType> order = new Collection<SPContentType>();

// add the content types we want to that collection
order.Add(reviewCT);
order.Add(resltsCT);

SPFolder folder = item.Folder;

// set the property to our collection
folder.UniqueContentTypeOrder = order;

// and update :)
folder.Update();

}

 

That should do the trick.  Now when a Collateral Folder is added this code will run and set that only the Review and Results content types should be on the new button.

Good luck!

-Chris.