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 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

share|improve this question
    
which SharePoint product are you using ? as you can only use "properties.ListItem.ContentType" in SharePoint 2013 :) –  Muhammad Raja Jan 16 '13 at 23:31

4 Answers 4

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks this works. –  Eugene Jan 18 '13 at 17:31

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.

share|improve this answer

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 :)

share|improve this answer
    
User clearly said he don't want to use "ItemAdded" however I would like to know why its slightly different in item added event :) –  Muhammad Raja Jan 16 '13 at 23:29
    
After reading his question I am pretty sure he knows whats the difference between adding and added and I don't think you needed to add code that I already added using ItemAdding in my answer which was required by User at first place :) –  Muhammad Raja Jan 16 '13 at 23:40

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.

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.