0

it's been a while since I've used Sitecore and I'm struggling with an issue.

There is a model which is being autogenerated by TDS and uses Glassmapper for its mapping.

[SitecoreType(TemplateId=IParametersTemplate_PanelConstants.TemplateIdString, Cachable=false)]
public partial interface IParametersTemplate_Panel : IGlassBase 
{
    [SitecoreField(IParametersTemplate_PanelConstants.BackGroundColorFieldName)]
    Guid BackGroundColor {get; set;}

    [SitecoreField(IParametersTemplate_PanelConstants.HasBackgroundColorFieldName)]
    bool HasBackgroundColor {get; set;}
}

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public static partial class IParametersTemplate_PanelConstants
{
    public const string TemplateIdString = "cbecbd24-8b2d-4694-a4f2-aa888b385cfc";
    public static readonly ID TemplateId = new ID(TemplateIdString);
    public const string TemplateName = "ParametersTemplate_Panel";

    public static readonly ID BackGroundColorFieldId = new ID("7760746c-8e96-4ef9-8d53-bf4428e18100");
    public const string BackGroundColorFieldName = "BackGroundColor";
    public static readonly ID HasBackgroundColorFieldId = new ID("b7fdfde1-258c-48a8-a934-3425a1c31494");
    public const string HasBackgroundColorFieldName = "HasBackgroundColor";

}

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[SitecoreType(TemplateId=IParametersTemplate_PanelConstants.TemplateIdString, Cachable=false)] 
public partial class ParametersTemplate_Panel : GlassBase, IParametersTemplate_Panel 
{
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Team Development for Sitecore - GlassItem.tt", "1.0")]
    [SitecoreField(IParametersTemplate_PanelConstants.BackGroundColorFieldName)]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Team Development for Sitecore - GlassItem.tt", "1.0")]
    [SitecoreField(IParametersTemplate_PanelConstants.HasBackgroundColorFieldName)]
    public virtual bool HasBackgroundColor {get; set;}
}

I've added a Droplink field to my template (figure 1.1) called BackGroundColor which points to a datasource that contains 3 items that have keys and values (figure 1.2).

figure 1.1 enter image description here

figure 1.2

enter image description here

I would like to get the selected item from the XP editor (figure 2.0) from my droplist so I can map its value (the color class name) to my model.

figure 2.0 enter image description here

It's not difficult for a checkbox or something similar but getting a selected item from a droplist seems to be a bit more challenging. Any ideas on how I can make this happen using the current setup ( seeing as my models are being autogenerated)?

thanks.

2
  • You are using Droplink not Droplist, right? Try to get Item from Guid and then access properties Jul 28 at 13:34
  • Yes, I am using a Droplink. But I'm not getting the item. Jul 28 at 13:53
2

There is no need to get the item through the Guid. There is a Custom Data property on the field (in this case BackGroundColor) in Visual Studio, you can set this to the type of color and the code gen process would generate the property as this Color class instead of a Guid, so you will put something like this:

type=<optional namespace or global namespace>.Color

A more detailed walkthrough about the Custom Data property and Code Generation can be found here

1
  • This look great. It works :). Thanks. Jul 29 at 13:12
0

You can create your custom partial class for this template like the following:

public partial class ParametersTemplatePanel
{
     [SitecoreField(IParametersTemplate_PanelConstants.BackGroundColorFieldName)]
     public virtual Color BackgroundColor { get; set; }
     [SitecoreField(IParametersTemplate_PanelConstants.HasBackgroundColorFieldName)]
     public virtual bool HasBackgroundColor { get; set; }   
}

You add here a using to the generated Color template which has the Key-Value properties. After using this class you will have always acces for Key-Value.

to get this template you will need like the following to be called:

var parameters = sitecoreContext.GetItem<ParametersTemplatePanel>(id);
var backgroundKey = parameters.BackgroundColor.Key;
var backgroundValue = parameters.BackgroundColor.Value;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.