Let’s Talk

We would love to hear from you. Want to know more about
our services or have any questions? Say Hi!

Using Glass Mapper Item/Datasource children getting null

September 22, 2022
Using Glass Mapper Item/Datasource children getting null
Keyur Garala
Keyur Garala
Sitecore Certified Architect
using-glass-mapper-item-datasource-children-getting-null
Summary

When I was trying to fetch the child of the datasource item, it is getting a null value for the child. After so many r&d I am getting to know about Lazy loading and this issue is coming because of enabling lazy loading. So, I am trying to turn off lazy loading by the below method.

[SitecoreChildren(IsLazy = false)]

IEnumerable Children{get;set;}

The above code is working only if you are using below v5 of glass mapper. But in version 5 or more (v5), the glass mapper has removed this method from here. So we can use an alternative method to turn off lazy loading. Please check the below code.

var datasource = _mvcContext.GetDataSourceItem(new Glass.Mapper.Sc.GetKnownOptions() { Lazy = Glass.Mapper.LazyLoading.Disabled });

I resolved my issue by doing this method.

These changes are working for me but after some days I am facing the below kind of error and it tells me to enable lazy loading, and by enabling lazy loading I do not get children of the datasource.

Below is my error please check.

Error:

using-glass-mapper-item-datasource-children-getting-null-1.png

Error in text:

Model depth check failed. Model graph too large, enable lazy loading. Type requested:

Sitecore.Data.Items.Itemlocal.Sc.Models.Templates.UserDefined.Main.Entities.Lists.IPublicationType
local.Sc.Models.Templates.UserDefined.Main.Entities.ExperienceAndAnalysis.IExperienceAndAnalysisBase
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.UserDefined.Main.Pages.ICapabilitiesLanding
Description: An unhandled exception occurred.

Exception Details: Glass.Mapper.MapperStackException: Model depth check failed. Model graph too large, enable lazy loading. Type requested:

Sitecore.Data.Items.Itemlocal.Sc.Models.Templates.UserDefined.Main.Entities.Lists.IPublicationType
local.Sc.Models.Templates.UserDefined.Main.Entities.ExperienceAndAnalysis.IExperienceAndAnalysisBase
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IService
local.Sc.Models.Templates.Starter.Content.Entities.Professionals.IProfessional
local.Sc.Models.Templates.Starter.Content.Entities.Services.IServicew
local.Sc.Models.Templates.UserDefined.Main.Pages.ICapabilitiesLanding

Solution:

By doing R&D, I got an option which is given in lazy loading “OnlyReferenced”. I applied this option on lazy loading and then my issue was resolved.

var datasource = _mvcContext.GetDataSourceItem<IInterfaceClass>(new Glass.Mapper.Sc.GetKnownOptions() { Lazy = Glass.Mapper.LazyLoading.OnlyReferenced }); 
                        

This error occurred because lazy loading disables the item nested child and so on till the 8th child comes after the 8th child will not be permitted.

Lazy loading disabled: Lazy loading is disabled for the current model and all referenced Glass models.

Lazy loading Only reference: Lazy loading is disabled for the current model but all referenced model will be lazy loaded.


YOU MAY ALSO LIKE