0

We migrated to Sitecore 10.1 and Glass Mapper is upgraded to 5.8.177.0 from 4.3.4.197.

I noticed the mapping was not working.

I changed the code from

var centreHours = glassContext.GetItem<TradingHoursModel>(Rendering.DataSourceItem.Fields["Centre Hours"].Value);

to

 var centreHours = _mvcContext.SitecoreService.GetItem<TradingHoursModel>(Rendering.DataSourceItem.Fields["Centre Hours"].Value);

The model was null and then I added x.LazyDisabled()

var centreHours =  _mvcContext.SitecoreService.GetItem<TradingHoursModel>(Rendering.DataSourceItem.Fields["Centre Hours Datasource"].Value, x => x.LazyDisabled());

It started working after x.LazyDisabled(). Do I need to pass x.LazyDisabled() in all the .GetItem method?

Since this is a data source item, I also tried the following code but it didn't work and the model was null.

_mvcContext.GetDataSourceItem<TradingHoursModel>(new GetKnownOptions { Lazy = Glass.Mapper.LazyLoading.Disabled});
4
  • Did you try like this? IMvcContext mvcContext = new MvcContext(); var mapItem= mvcContext.SitecoreService.GetItem<IMap>(DataSourceItem); Dec 6, 2021 at 3:42
  • @GauravAgarwal yeah this is what I tried but didn;t work _mvcContext.SitecoreService.GetItem<TradingHoursModel>(Rendering.DataSourceItem.Fields["Centre Hours"].Value); Then, I passed x.LazyDisabled() and it starts working Dec 6, 2021 at 3:45
  • Is there any security applied on the datasource item? I had a similar issue and reported this issue github.com/mikeedwards83/Glass.Mapper/issues/440
    – Jeroen
    Dec 6, 2021 at 15:19
  • @Jeroen No there is not security applied in my scenario. Dec 6, 2021 at 23:07

1 Answer 1

3

I assume that you have written a generic class for all the MVCContent Sitecore service methods. If you don't want to pass x.LazyDisabled() in all the .GetItem methods, you can modify your code as below:

  public T GetItem<T>(GetItemOptions options) where T : class
    {
        options.Lazy = Glass.Mapper.LazyLoading.Disabled;
        return _mvcContext.SitecoreService.GetItem<T>(options);
    }

Other options with LazyLoading:

public enum LazyLoading
{
    Enabled = 0,
    Disabled = 10,
    OnlyReferenced = 20
}

Update:

Refer to below sample code to create a generic class:

 public class RenderingRepository : IRenderingRepository
 {
    private readonly IMvcContext _mvcContext;

    public RenderingRepository(IMvcContext mvcContext)
    {
        _mvcContext = mvcContext;
    }
    public T GetItem<T>(GetItemOptions options) where T : class
    {
        options.Lazy = Glass.Mapper.LazyLoading.Disabled;
        return _mvcContext.SitecoreService.GetItem<T>(options);
    }
}

Register it into your DI:

 serviceCollection.AddTransient<IRenderingRepository, RenderingRepository>();

Now using Controller Constructor DI, you can use this method in your controller Action Result:

public class SampleController : SitecoreController
{
    private readonly IRenderingRepository _renderingRepository;
    public Sample(IRenderingRepository renderingRepository)
    {
        _renderingRepository = renderingRepository;
    }
    public ActionResult Index()
    {
        return View(_renderingRepository.MethodName());
    }
}
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.