2

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?

2 Answers 2

2

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.

2
  • 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, 2013 at 12:18
  • I agree and glad that was able to help :) Jan 23, 2013 at 13:18
0

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"])

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.