22
Dec
11

How to set the default ContentType at a Library

 

Get All ContentTypes of the List.

List<SPContentType> ctl = new List<SPContentType>();
SPContentTypeCollection collection = list.ContentTypes;

Iterate over all ContentTypes of the List and compare your CustomContentType with all existing ContentTypes of the List. Compare the Name, not the ID.

Your CustomContentType has to be added to the List programmatically first.

Add the found/calculated ContentType to the List<SPContentType> ctl.

This is necassary in this manner, because after adding the ContentType to the List it gets a ID Appendix which you have to calculate first.

So if you would add your Custom ContentType directly to the List<SPContentType> ctl, it wouldn`t work, because exactly that ContentType is not existent at the SPList. When you add the ContentType to the SPList, it derives and has it`s own ID. So you first have to calculate the exact existent ContenType.

The complete Method looks like this:

private static void SetDefaultContentType(SPContentType CustomContentType, SPList list)
{
try
{
List<SPContentType> ctl = new List<SPContentType>();

SPContentTypeCollection collection = list.ContentTypes;

foreach (SPContentType ct in collection)
{
if (ct.Name.Contains(CustomContentType.Name))
{
ctl.Add(ct);
}
}

list.RootFolder.UniqueContentTypeOrder = ctl;
list.RootFolder.Update();
}
catch { }
}


December 2011
M T W T F S S
« Jun    
 1234
567891011
12131415161718
19202122232425
262728293031  

Badge Farm

  • Powered by Redoable 1.0



0 Responses to “How to set the default ContentType at a Library”


  1. No Comments

Leave a Reply