Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore Glass Mapper always null

I am using Sitecore Glass Mapper for a new project I'm setting up. We are using Sitecore 7.2, latest version of Team Development for Sitecore (TDS) code generation and the latest version of glass.

The code I am trying to execute:

var b = new SitecoreContext();
var c = b.GetCurrentItem<T01_Homepage>();

b is not null. c is null.

var d = b.GetItem<T01_Homepage>("path")

d is null.

I added my assembly in GlassMapperScCustom:

public static IConfigurationLoader[] GlassLoaders(){            

    var attributes = new AttributeConfigurationLoader(new[] { "Company.Framework.Websites.Corporate", "Company.Framework.Core", "Company.Framework.Common" });

    return new IConfigurationLoader[] { attributes };
}

When I look into b.GlassContext.TypeConfigurations all my models are there.

I figured it could be a language issue because the site is in dutch and maybe the wrong language would be resolved incorrectly. This was also not the case.

I disabled WebActivator and added the GlassMapperSc.Start() in my Global.asax Application_Start method.

We are also using Autofac as DI framework. But without it, it still isn't working as you can see above. Also when I create my own custom models without TDS code generation the result of GetCurrentItem<T> is null.

Does anyone have an idea how I can fix this?

like image 201
Hans Leautaud Avatar asked Apr 24 '14 09:04

Hans Leautaud


2 Answers

Did you check your Sites.config and the default language for this website? There could be a difference between the language which is defined in your Sitecore languages folder and your configuration.

I had a similar problem with one of my projects where I changed the Sitecore.Context.Language to "nl" instead of "nl-NL". The glass mapper will return null, but Sitecore.Context.Database.GetItem will return an object in this case.

like image 82
Aldentev Avatar answered Dec 15 '22 10:12

Aldentev


Most of the times it is a language issue. The mapper returns a null object when you do not have versions in the current or given language.

What can be confusing is that Sitecore.Context.Database.GetItem returns an object, even if it does not have a version in the current language. Be sure to check that item.Versions has any.

like image 44
RvanDalen Avatar answered Dec 15 '22 11:12

RvanDalen