Question

I need to change a field in the item being added based on its content type. This needs to happen before the item has been added - hence ItemAdding event receiver. Using ItemAdded is no good since the user will see the original value and would have to refresh the page to see the new value - the same happens when I try to use a workflow to do this.

So is there a way to obtain the ContentType of the item being added in ItemAdding Event Handler?

Thanks

Was it helpful?

Solution

Yup you can get content type of item using following code,

public override void ItemAdding(SPItemEventProperties properties)
{
    SPList list = properties.List; 
    string contentTypeName = properties.AfterProperties["ContentType"].ToString(); 
    SPContentType contentType = list.ContentTypes[contentTypeName]; 
}

For more information check this link out.

OTHER TIPS

Actual Results from Running Debug

SP 2010 - Custom Content Type Event Receiver in a Document Library - override ItemAdding ...querying the immediate window ? properties.ListItem.ContentType 'properties.ListItem' is null ? properties.AfterProperties["ContentTypeId"] "0x0120D520003BBF99EB54C34597ACF700732E3882F600AA09EB683FFEB74BA408BE00002EB8DE" ? properties.AfterProperties["HTML_x0020_File_x0020_Type"] "SharePoint.DocumentSet"

I'm using a Document Library (with the Document Set feature activated) so I have the HTML_x0020_File_x0020_Type hash-key to give me the English description I'm looking for. On an item list that key probably doesn't exist, so you only have the hash-key ContentTypeId to work with.

You can simply do it by doing:

Gets a value that specifies the content type of the list item.

Returns a ContentType.instance representing the content type of the list item.

SPContentType = properties.ListItem.ContentType;

so for item adding it will be:

public override void ItemAdding(SPItemEventProperties properties)
{
    SPContentType ct = properties.ListItem.ContentType;
}

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.listitem.contenttype.aspx

Just for clarification for item added event for others:

In the item added event its slightly different

public override void ItemAdded(SPItemEventProperties properties)
{

    SPList list = properties.listName;  

    string ctName = properties.AfterProperties["ContentType"].ToString();  

    SPContentType contentType = list.ContentTypes[ctName]; 

}

hope it helps :)

The above code did not work for me, properties.AfterProperties("ContentType") was still nothing within ItemAdding (SharePoint 2010). However, properties.AfterProperties("ContentTypeId") worked. Just wanted to share in case anyone else stumbled upon this with the same issue as me.

        Dim currentContentTypeId As New SPContentTypeId(properties.AfterProperties("ContentTypeId").ToString)
        Dim currentContentType As SPContentType = properties.List.ContentTypes(currentContentTypeId)

        If currentContentType.Name = "MyContentType" Then
            'Do stuff
        End If

Note that I used properties.**List**.ContentTypes(...) and not properties.**Web**.ContentTypes(...). The item being added will have the ContentTypeId specific to the current list, which is not the same as the ContentTypeId in the site.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top