Sitecore Glass Mapper always null
Asked Answered
M

3

6

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?

Modern answered 24/4, 2014 at 9:25 Comment(0)
E
2

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.

Enumeration answered 24/4, 2014 at 10:53 Comment(4)
That did the trick. My language was nl-nl and in sites.config the sites/site node had the attribute language on nl. Thanks.Modern
This is interesting - I am having a similar problem, and indeed when I inspect the root of my website the language seems to be set to "hr" - a value that I never explicitely set, but it's the name of a first-level child of my website that I have since removed: screencast.com/t/aMhy7yl3 Could this have somehow made Sitecore/Glass.Mapper think that "hr" (Croatian) should be the language, thus returning a null item?Uttica
@HansLeautaud where do I find Sites.config in inetpub folder?Worthy
@Sidso, I do not know anymore. The topic is 6 years old.Modern
B
1

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.

Barn answered 24/4, 2014 at 9:44 Comment(1)
Thanks, this response in combination with the response of Aldentev did the trick for me.Modern
E
0

Some things you may try (this didn't fit in the comments field)

1) Confirm that the related fields in the Sitecore Item object contain values (so Sitecore.Context.Item for your "c" var and Sitecore.Context.Database.GetItem("path") for your "d" var)

2) Try to encapsulate the GetItem/GetCurrentItem call in a VersionCountDisabler, like this:

T01_Homepage model = null;

using (new VersionCountDisabler())
{
  var context = new SitecoreContext();
  model = context.GetItem<T01_Homepage>("path");
}

// Do you have data in model now?

3) Try to encapsulate the same call with a SecurityDisabler. Just to confirm it's not a security issue.

4) If you still don't know what it is, please update your question with some (simplified) code for your model.

Eelpout answered 24/4, 2014 at 10:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.