Device Fields

Device Fields

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Collections.FieldCollection
  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Fields.Field
  • Sitecore.Data.Items.DeviceItem
  • Sitecore.Data.Items.Item
  • Sitecore.Layouts.DeviceDefinition
  • Sitecore.Layouts.LayoutDefinition
  • System.Collections.ArrayList

Device Fields C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

string renderings = item.Fields["__renderings"].Value;
Sitecore.Layouts.LayoutDefinition layoutDef = Sitecore.Layouts.LayoutDefinition.Parse(renderings);
if (layoutDef == null) return;

System.Collections.ArrayList devices = layoutDef.Devices;
if (devices == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

foreach (Sitecore.Layouts.DeviceDefinition device in devices)
{
    string deviceID = device.ID.ToString();
    Sitecore.Data.Items.DeviceItem deviceItem = db.Resources.Devices[deviceID];
    string deviceName = deviceItem.DisplayName;
    string fieldsCount = deviceItem.InnerItem.Fields.Count.ToString();
    output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
    output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
    output.Write("{0} - {1}", deviceName + " Device", fieldsCount + " fields");
    output.RenderEndTag(); //P
    
    Sitecore.Collections.FieldCollection deviceFields = deviceItem.InnerItem.Fields;
    foreach (Sitecore.Data.Fields.Field deviceFld in deviceFields)
    {
        output.Write("{0}&#58;&#160;{1}", "   Field Name", "<b>" + deviceFld.DisplayName + "</b><br/>");  
    }
}
output.RenderEndTag(); //Div


Device Fields C# Web Control Output

Default Device - 4 fields

Field Name: __Icon
Field Name: Default
Field Name: __Help link
Field Name: __Sortorder

Print Device - 3 fields

Field Name: __Icon
Field Name: Query string
Field Name: __Sortorder

Feed Device - 3 fields

Field Name: Fallback device
Field Name: __Icon
Field Name: Query string

Notes

  • Assign the value of the _renderings field from the Standard Template for the currently requested item in the Content Tree to a string variable
  • Check to see if the string contains a null or empty value before attempting to process it
This snippet uses a fields collection to reference the fields
  • Returns null if a field does not exist or the field value is null
  • Does not generate inline editing controls for the Page Editor
  • Useful for field types other than simple field types