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; }
Related
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
We have recently upgraded our application from Glass mapper v3 to v.4.0.9.60 and facing this error 'Page editing error. Could not find property on type System.Object' on all properties in Edit mode. It works fine in live mode or preview mode.
Here are the details of our Glass mapper implementation:
Domain classes:
[SitecoreType(TemplateId = "{XXXXX}", AutoMap = true)]
public interface CompanyInformationPage : BasePage
{
string CompanyName { get; set; }
}
[SitecoreType(TemplateId = "{XXXXX}", AutoMap = true)]
public interface BasePage : ISitecoreItem
{
string Title { get; set; }
string Description { get; set; }
string Summary { get; set; }
Image Image { get; set; }
}
View:
#inherits Glass.Mapper.Sc.Web.Mvc.GlassView<CompanyInformationPage>
<div class="company_page">
<h1>#Editable(Model, x=>x.Title)</h1>
#Html.Raw(#Editable(Model, x => x.Description))
</div>
We have tried removing GlassView as mentioned under http://glass.lu/Blog/GlassV4 (MVC Improvements) section but it didn't fix this issue.
We have also tried enabling z.Glass.Mapper.Sc.ViewRender.config.exclude file as per #198 but this also didn't work. We just renamed this file and removed '.exclude' extension and didn't make any changes in GlassMapperScCustom.cs file. Are we missing anything here?
Please advise.
Thank you!
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
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;
I have been looking into document databases, specifically RavenDb, and all the examples are clear and understandable. I just can't find any example where we do not know beforehand how many levels a given structure has. As an example how would you persist a family tree given the following class:
public class Person{
public string Name {get;set;}
public Person Parent {get;set;}
public Person[] Children {get;set;}
}
In most examples I have seen we search for the aggregate root and make into a document. It is just not so obvious here what the aggregate root and boundary is.
Ayende has just posted a blog post that answers this.
I guess for RavenDb, you'd have to keep the Ids in your object:
public class Person {
public string Name { get; set; }
public string ParentId { get; set; }
public string[] ChildrenIds { get; set; }
}
Check this page, especially at the bottom, for more info: http://ravendb.net/documentation/docs-document-design