Developer FAQ
How to create a tree structure of elements programmatically?
Answer:
This sample code illustrates how to create a tree structure in C1:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Composite.C1Console.Elements; using Composite.C1Console.Elements.Plugins.ElementProvider; using Composite.C1Console.Security; using Composite.C1Console.Security.SecurityAncestorProviders; using Composite.Core.ResourceSystem.Icons; namespace MyCode { public sealed class EnvironmentPropertiesElementProvider : IHooklessElementProvider, IAuxiliarySecurityAncestorProvider { private EnvironmentPropertiesElements _environmentPropertiesElements; private IEnumerable<Element> Roots { get { yield return _environmentPropertiesElements.EnvironmentElement; } } public EnvironmentPropertiesElementProvider() { AuxiliarySecurityAncestorFacade.AddAuxiliaryAncestorProvider<EnvironmentEntityToken>(this); } public ElementProviderContext Context { set { _environmentPropertiesElements = new EnvironmentPropertiesElements(value); } } public IEnumerable<Element> GetChildren(EntityToken entityToken, SearchToken seachToken) { if (entityToken.Id == _environmentPropertiesElements.EnvironmentElementId.ToString()) { yield return _environmentPropertiesElements.OSVersionElement; yield return _environmentPropertiesElements.ProcessorCountElement; } yield break; } public IEnumerable<Element> GetRoots(SearchToken seachToken) { return Roots; } public Dictionary<EntityToken, IEnumerable<EntityToken>> GetParents(IEnumerable<EntityToken> entityTokens) { var result = new Dictionary<EntityToken, IEnumerable<EntityToken>>(); foreach (EntityToken entityToken in entityTokens) { if (entityToken.Id == _environmentPropertiesElements.EnvironmentElementId.ToString()) continue; result.Add(entityToken, new EntityToken[] { _environmentPropertiesElements.EnvironmentEntityToken }); } return result; } } internal class EnvironmentPropertiesElements { private ElementProviderContext _providerContext; public EnvironmentPropertiesElements(ElementProviderContext value) { this._providerContext = value; } public Element EnvironmentElement { get { return new Element(_providerContext.CreateElementHandle(EnvironmentEntityToken)) { VisualData = new ElementVisualizedData() { Label = "Environment", ToolTip = "Environment", HasChildren = true, Icon = CommonElementIcons.Folder, OpenedIcon = CommonElementIcons.FolderOpen } }; } } public Element OSVersionElement { get { return new Element(_providerContext.CreateElementHandle(new EnvironmentEntityToken("OSVersion"))) { VisualData = new ElementVisualizedData() { Label = Environment.OSVersion.ToString(), ToolTip = "ProcessorCount", HasChildren = false, Icon = CommonElementIcons.Data, OpenedIcon = CommonElementIcons.Data } }; } } public Element ProcessorCountElement { get { return new Element(_providerContext.CreateElementHandle(new EnvironmentEntityToken("ProcessorCount"))) { VisualData = new ElementVisualizedData() { Label = Environment.ProcessorCount.ToString(), ToolTip = "ProcessorCount", HasChildren = false, Icon = CommonElementIcons.Data, OpenedIcon = CommonElementIcons.Data } }; } } public EntityToken EnvironmentEntityToken { get { return new EnvironmentEntityToken(EnvironmentElementId.ToString()); } } public Guid EnvironmentElementId { get { return new Guid("{CABEDE97-FB1C-48F5-847D-16C7675EB889}"); } } } [SecurityAncestorProvider(typeof(NoAncestorSecurityAncestorProvider))] public class EnvironmentEntityToken : EntityToken { private string _id; public EnvironmentEntityToken(string id) { _id = id; } public override string Id { get { return _id; } } public override string Serialize() { return DoSerialize(); } public override string Source { get { return ""; } } public override string Type { get { return ""; } } public static EntityToken Deserialize(string serializeedEntityToken) { string type, source, id; DoDeserialize(serializeedEntityToken, out type, out source, out id); return new EnvironmentEntityToken(id); } } }
To use this sample:
- Copy this file to
/App_Code
. - Edit Composite.config and locate this element:
Composite.C1Console.Elements.Plugins.ElementProviderConfiguration[@rootProviderName="VirtualElementProvider"]/
.
ElementProviderPlugins/add[@name="VirtualElementProvider"]/VirtualElements - Under this element, add the following:
<add id="SystemPerspective" order="8" parentId="ID01" tag="System" providerName="EnvironmentPropertiesElementProvider" label="${Composite.Management, VirtualElementProviderElementProvider.SystemPerspective}" closeFolderIconName="Composite.Icons.perspective-system" openFolderIconName="" type="Composite.Plugins.Elements.ElementProviders.VirtualElementProvider.ProviderHookingElementConfigurationElement, Composite" name="EnvironmentProperties" />
- Then, under
Composite.C1Console.Elements.Plugins.ElementProviderConfiguration[@rootProviderName="VirtualElementProvider"]/
, add the following:
ElementProviderPlugins<add name="EnvironmentPropertiesElementProvider" type="MyCode.EnvironmentPropertiesElementProvider, App_Code" />
- Now log in to the C1 Console.
- Restart the server (Tools | Restart Server).
- Switch to the System perspective.
There should be a new element: Environment, that has two sub-elements (OS, the number of processor cores).