Sitecore Glass Mapper Get All Siblings - sitecore

I am trying to get all items at the current item level. I am using Glass Mapper SitecoreQuery for the same. I am able to get the current item but not able to map all siblings
public class TestModel:BaseModel
{
[SitecoreQuery("../*")]
public virtual IEnumerable<Model1> Siblings { get; set; }
}
[SitecoreType(AutoMap = true)]
public class Model1 : BaseModel
{
}
Base Model has all the required fields and correctly mapped. I am actually trying to display all items at the level of current item.

Add second parameter to SitecoreQuery : IsRelative = true like that:
[SitecoreQuery("../*", IsRelative = true)]
public virtual IEnumerable<Model1> Siblings { get; set; }
It tells Sitecore to start query at your item level instead of starting at the tree root.
You can find more information in the Official Sitecore Glass Mapper Tutorial

Related

Glass Mapper Droplink Show Name instead of Guid

I'm using glass mapper and my template has a droplink list in it. When i attempt to retreive the item, it's bringing back the selected guid in the droplink instead of the name. How do I go about showing the name selected in the droplink versus the guid?
If you'd like the name of the item instead of the GUID, why not use a Droplist? Or do you need the GUID for other purposes? Glass is simply returning what Sitecore is actually storing in this case (which for a Droplink would be the item GUID).
Otherwise, you should create a new class for the linked item which includes the item name, and then change your mapped property to return that class instead of a string.
FYI, though a Droplist might be the easier fix, use of that field type is not a good practice. Since the item name is stored instead of the GUID, changes to that item name do not cascade to linking items.
I am giving a working example for techphoria414's answer for future use.
Imagine your droplink field contains the items of KeyValuePair type:
[SitecoreType(TemplateId = "Id")]
public partial interface IKeyValuePair
{
[SitecoreField(FieldId = "Id")]
string Key { get; set; }
[SitecoreField(FieldId = "Id")]
string Value { get; set; }
}
If your droplink field is SelectColourBar which is from template ColourBar
[SitecoreType(TemplateId = "Id")]
public partial interface IColourBar
{
[SitecoreField(FieldId = "Id")]
IKeyValuePair SelectColourBar { get; set; }
}
This will map SelectColourBar to KeyValuePair model, then one can access key or value.

Logging/Debugging Mapping Errors in Glass Mapper SC 4.0.2.10

Does anyone know of a way to force Glass Mapper SC to throw exceptions for mapping errors? It appears to swallow them, and I'm left with null properties and no easy way to diagnose the problem. The tutorials don't really dive deep into attribute configuration, so I'm forced to do a lot of TIAS which slows down development.
I'd also settle for any method that other users have found helpful for diagnosing mapping issues.
Example
Here is the template for the items I'm retrieving and attempting to map:
Here is one of the items that I am returning with my query:
Here is the model that matches the template:
[SitecoreType(AutoMap = true)]
public class UnitDetails
{
//[SitecoreField("ID"), SitecoreId]
public virtual Guid ID { get; set; }
[SitecoreField("Pre-Recycled Percentage")]
public virtual decimal PreConsumerRecycledPercentage { get; set; }
[SitecoreField("Post-Recycled Percentage")]
public virtual decimal PostConsumerRecycledPercentage { get; set; }
public virtual Plant Plant { get; set; }
[SitecoreField("Raw Material")]
public virtual RawMaterial RawMaterial { get; set; }
[SitecoreField("Raw Material Origin")]
public virtual RawMaterialOrigin RawMaterialOrigin { get; set; }
}
Even if you forget the RawMaterial and RawMaterialOrigin properties for the moment (those don't map either), the decimal properties do not map. Also, the ID property will always be null unless I name it exactly (ID). I thought the [SitecoreField("ID"), SitecoreId] decorator was supposed to provide the hint to Glass. Here is an example of the mapped data. No exception is thrown:
I understand this is old thread and might have resolved already, but as I managed to resolve this one more time (forgot to update last time :D) thought of recording this time.
I was doing upgrade to v5 of glass mapper. I followed attribute based configuration which is default. It is documented here, but on top of that I add
1) Templates on classes
[SitecoreType(AutoMap = true, TemplateId = "<Branch Id>"]
2) Id field should be declared as following in your code.
[SitecoreId]
public virtual Guid Id { get; set; }
3) Sitecore service changes as mentioned in the article using Sitecore Service (MVC / WebForm), passed lazy load as false and infer type as true in all places. This was really important step.
I hope this will help me next time I visit this issue. :D

Use Sitecore / GlassView RenderImage with an item (MVC)

I have a bunch of items in View that aren't fields on the model and just items. I really want to user RenderImage so I don't have to re-invent all the html code, but it really wants an item or and item of the GlassView type.
Is there a simple way to just force feed an item into GlassView.RenderImage?
How about sitecore's Render Image? It just wants a field value, but I want to give it an item?
You should add a new model which extends the existing (Sitecore template based) one.
For example you have the IArticle model, which has every field of the item, but not much else, as usual. You should create a new model, which inherits from the original, and you can add new fields, which will be mapped by Glass, if set properly. You can use the following attributes for example:
[SitecoreNode] (define an item by id or path)
[SitecoreParent] (by hierarchy)
[SitecoreQuery] (sitecore query)
[SitecoreChildren] (hierarchy)
Models
/// This model is based on the Sitecore template
[SitecoreType(TemplateId = "something")]
public interface IArticle : IBaseItem {
[SitecoreField]
string Title { get; set; }
[SitecoreField]
string Content { get; set; }
}
/// This model defines additional items.
public interface IArticleDetail : IArticle {
[SitecoreNode(Id = Constants.MainBannerId)]
IBanner PromoBanner { get; set; }
[SitecoreQuery("somequery")]
IEnumerable<ITag> Tags { get; set; }
}
In this case you GlassView inherits from the IArticleDetail, and the model binder propagates the additional fields as well.
If you want to render (editable images), you can just use the following syntax:
#RenderImage(Model.PromoBanner, m => BannerImage, isEditable: true)
or
#Html.Glass().RenderImage(Model.PromoBanner, m => BannerImage, isEditable: true)
#RenderImage will only accept a type of Glass.Mapper.Sc.Fields.Image but if you have a property on your view of Item, I would suggest just using the standard HTML helper field render:
#Html.Sitecore().Field("Field Name", Model.SomeSubItem)

Sitecore Glass Childlist Runtime Error

I'm working in a Sitecore project that uses Glass and code generation to create glass classes. I wanted an easy way to get a child list on every Glass class type so on IGlassBase I added
IEnumerable<GlassBase> Children { get; set; }
and on GlassBase
[SitecoreChildren]
public virtual IEnumerable<GlassBase> Children { get; set; }
but I am getting a runtime error saying that Children cannot be added twice. Any ideas?
Try to add "SitecoreChildren" to your interface rather than your concrete class, like:
[SitecoreChildren]
IEnumerable<GlassBase> Children { get; set; }
This is how i have it on all of my projects, and it works fine.
hope this helps
You Can try like this:
[SitecoreChildren(InferType = true)]
IEnumerable<GlassBase> Children { get; set; }

How to implement Sitecore Template inheritance with Glass Mapper

I've been trying to achieve the following with glass mapper but can't get it to work.
I have a Home Page template which doesn't have any fields itself but inherits the following two templates:
Navigation Template
Fields: Navigation Title
Meta Information Template
Fields: Page Title, Meta Description
I've created the corresponding interfaces / classes as follows:
[SitecoreType(TemplateId = "{5BAB563C-12AD-4398-8C4A-BF623F7DBCDC}", AutoMap = true)]
public interface INavigation
{
[SitecoreField(FieldName = "Navigation Title")]
string NavigationTitle { get; set; }
}
[SitecoreType(TemplateId = "{95539498-31A5-4CB5-8DD6-C422D505C482}", AutoMap = true)]
public interface IMetaInformation
{
[SitecoreField]
string PageTitle { get; set; }
[SitecoreField]
string MetaDescription { get; set; }
}
[SitecoreType(TemplateId = "{F08693E5-8660-4B13-BBD6-7B9DC6091750}", AutoMap = true)]
public class HomePage : INavigation, IMetaInformation
{
public virtual string NavigationTitle { get; set; }
public virtual string PageTitle { get; set; }
public virtual string MetaDescription { get; set; }
}
When I then try accessing my page all attributes are always null:
var context = new SitecoreContext();
var page = context.GetCurrentItem<HomePage>();
I've tried several different approaches to this but nothing works. Also what was described in different tutorials didn't work. The only thing that works is when I add the fields directly on the Home Page template, but I don't want that since I have more than one page type and I therefore want to inherit the fields.
Does anyone have any idea what I'm missing here?! I'm using Sitecore 7 with .NET 4.5 by the way if that makes a difference.
Your fields are not mapped because you use a space in the Fieldname in the Sitecore Template.
Either remove the space or add the attribute [SitecoreField(FieldName ="Page Title")] to the Model.
I think that the Homepage class is trying to map the NavigationTitle on the Homepage template with the fieldName NavigationTitle and ignores the FieldName attribute on the base model.
By the way: I am using only interfaces for the current project I'm working on and it works as expected with inheritance. No need to add a property more then once ;)
Try to set infer type to true. I cannot get it to work at all without having that set.
ex.
item.GlassCast<HomePage>(false, true);
or
context.GetCurrentItem<HomePage>(false, true);
I find it does not work without this set.
You should render the common fields in a separate sublayout as a GlassUserControl.
public partial class NavigationTemplate : GlassUserControl<NavigationTemplate>
{
protected void Page_Load(object sender, EventArgs e)
Here you will have direct access to the NavigationTemplate fields no matter what item you are loading, it will always be cast to NavigationTemplate and will read the field values of the item you are loading.
It seems that you expect to get the properties on the HomePage instance, but you need to ask for the exact interface that contains the property as seen here
I.e.
Instead of doing:
var page = context.GetCurrentItem<HomePage>();
You should explicitly get current item as INavigation and get the field from the interface:
var navigationTitle = context.GetCurrentItem<INavigation>().NavigationTitle;