How to determine item ContentType in FormControl (FieldIterator for instance)?
-
10-12-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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"])