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've implemented my ListFieldIterator to change the way field controls rendered. I'd like to change the behaviour of form depending on the item content type, but any attempts to determine ContentTypeId from the code returns wrong result.

ListFieldIterator.ListItem.ContentTypeId

SPContext.Current.ListItem.ContentTypeId

are always return

0x00C2208B8CE6E1422CADC1C521EAB2A68B

(which is invalid content type id)

There's a property LoadContentTypes in SPContext class, but setting it TRUE for SPContext.Current or FieldIterator.ItemContext makes no difference.

What am I doing wrong?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

In addition to the method with determining ContentType in custom ListFieldIterator from query string, ItemContext contains property ContentType. But this property is internal and therefore could not be used directly in custom ListFieldIterator implementation.

Below is presented wrapper for retriving ContentType based on corresponding property for SPContext (Reflection is used here)

        private SPContentType CurrentContentType
        {
            get {
                var ct = typeof(SPContext).GetProperty("ContentType", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ItemContext, null) as SPContentType;
                return ct;
            }
        }

Using CurrentContentType property, content type could be determined in custom ListFieldIterator implementation.

share|improve this answer
    
Thanks a lot! Your solution actually the best one, because it allows to determine content type even if it isn't specified in query string. –  Leff Jan 23 '13 at 12:18
    
I agree and glad that was able to help :) –  Vadim Gremyachev Jan 23 '13 at 13:18
add comment

The only way I found to determine content type in this case is to analize HTTP query string:

new SPContentTypeId(this.Context.Request.QueryString["ContentTypeId"])
share|improve this answer
add comment

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.