Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have an event-handler feature that changes the content type of an item when it is added to a document library, however this will only work if the content type is allowed for that document library. My feature needs to be able to create this content type if it does not already exist before assigning it to an item, however I most likely will also need to add it to the specific document libraries before it becomes usable.

Is it is simple as just adding the content type to the list of the content types for the document library? :

 SPListItem listItem = properties.ListItem;
 SPList list = properties.ListItem.ParentList;

 SPContentType baseContentType = web.ContentTypes[SPBuiltInContentTypeId.Document];
 SPContentType type = new SPContentType(baseContentType, web.ContentTypes, "Custom Folder");
 type.Fields.Add("Users", SPFieldType.User, false);
 list.ContentTypes.Add(type);
 list.Update();

Side question (bonus points ;) - The "Users" field above should be a multi-select people-picker field. Will the above work to create this field?

Thanks

share|improve this question
    
Side question (bonus points ;) - The "Users" field above should be a multi-select people-picker field. Will the above work to create this field? –  Music Magi Jun 6 '11 at 13:33

3 Answers 3

up vote 4 down vote accepted

You'll need to add the field to the content type using SPContentType.FieldLinks - as SPContentType.Fields is read-only.

As for creating the content type, I recommend first creating it at the RootWeb level, as a Site content type. Get the parent one from SPWeb.AvailableContentTypes. Add your field links. Then add this new content type to the SPList.ContentTypes collection (using SPList.ContentTypes.Add()).

share|improve this answer
    
How do I build the SPField and SPFieldLink objects before adding them to the content type? Is there a good resource for this you're aware of? I'm wondering what to fill in SpFieldCollection when i go to create an SPField object. Thanks –  Music Magi Jun 6 '11 at 13:43
    
SPSite.Fields.Add() will add your field to Site Collection Columns (Generally good practice) and return the internal name of your SPField so you can then return it from SPSite.Fields, and add it to your SPContentType.Fields collection. If it already exists at the site collection level, you no longer need to recreate it. Part of the purpose of using Content Types is to reuse fields as well as content types wherever possible –  James Love Jun 6 '11 at 13:46

I have the similar post in my blog - How to add Content Type programmatically. In this article I also demonstrate how to add fields to the new content type. Thanks!

share|improve this answer
SPContentType wfTask = web.ContentTypes["Workflow Task"];

web.AllowUnsafeUpdates = true;
SPList documentLibrary = web.Lists.TryGetList(libraryName);
documentLibrary.ContentTypesEnabled = true;
documentLibrary.ContentTypes.Add(wfTask);
documentLibrary.Update();
web.AllowUnsafeUpdates = false;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.