Come determinare il tipo di contenuto dell'articolo nel controllo del modulo (campo iteratore per esempio)?
-
10-12-2019 - |
Domanda
Ho implementato il mio listfierreator per cambiare il modo in cui i controlli sul campo rendono. Mi piacerebbe cambiare il comportamento della forma in base al tipo di contenuto dell'articolo, Ma tutti i tentativi di determinare il contenttypeid dal codice restituiscono risultati errati.
ListFieldIterator.ListItem.ContentTypeId
SPContext.Current.ListItem.ContentTypeId
.
sono sempre restituiti
.0x00C2208B8CE6E1422CADC1C521EAB2A68B
(che è il tipo di identificazione del tipo non valido)
C'è una proprietà LoadContentTypes in classe di SPContext, ma l'impostazione TRUE per
SPContext.Current
oFieldIterator.ItemContext
non fa differenza.Cosa sto facendo male?
Soluzione
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.
Altri suggerimenti
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"])