3

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

1
  • 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, 2011 at 13:33

3 Answers 3

4

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()).

2
  • 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, 2011 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, 2011 at 13:46
2
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;
1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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