Getting the ContentType from an ItemAdding Event Handler
Inside an event handler for an ItemAdded event, getting the List Item’s ContentType is as easy as
var contentType = properties.ListItem.ContentType;
However, for ItemAdding, properties.ListItem is null.
Luckily, the ContentType name and Id are part of the properties.AfterProperties collection – the following will work:
SPList list = properties.List; string contentTypeName = properties.AfterProperties["ContentType"].ToString(); SPContentType contentType = list.ContentTypes[contentTypeName]; SPFieldCollection fields = contentType.Fields;
Between the AfterProperties field values and the content type field collection, you typically have all that you need... Just remember to not depend on properties.ListItem.
Labels: c#, howto, microsoft, moss, sharepoint, SharePoint2010