1

We recently upgraded our Sitecore to 9.1 and also Glassmapper5.4.21 along with it. What we noticed is that some of the view renderings have a data source that does not have a version in a particular language.

While it worked in the older version, in the new version this throws an exception as below. the only workaround we found is to go and create versions for those items. (even though we did not need them in that language) .

> The model item passed into the dictionary is of type
> 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary
> requires a model item of type 'MyProject.IHeaderAndCopy'. Description:
> An unhandled exception occurred. 
> 
> Exception Details: System.InvalidOperationException: The model item
> passed into the dictionary is of type
> 'Sitecore.Mvc.Presentation.RenderingModel', but this dictionary
> requires a model item of type 'MyNamespace.Models.IHeaderAndCopy'.

my View rendering code

@model IHeaderAndCopy
@if (Model != null)
{

<h2>@Html.Glass().Editable(x => x.Header)</h2>

}

Any idea how to fix this without manually adding versions? Since this is an upgrade, this component (and happens with all other components) is being used all over, it will be very difficult to go add versions all over across all languages.

5
  • Good for you. 1) Why would you proceed to render an item version that doesn't exist? 2) Is there a question here somewhere? Jun 19, 2019 at 8:32
  • which glass version? Jun 19, 2019 at 11:16
  • 1
    @MarkCassidy this is because of the rendering being part of shared layout. The rendering is not being used in only one language. so only the content item only in that language has no version. it works fine in languages that has version. The question is obviously how to fix it :) Jun 19, 2019 at 12:26
  • @MichaelEdwards its 5.4.21. sorry missed that. updated in the question. Jun 19, 2019 at 12:27
  • @MichaelEdwards do you have any suggestions on what this might be? or potential solutions?
    – Svenkle
    Jul 25, 2019 at 2:51

2 Answers 2

5

It sounds like you are a victim of the change in the EnforceVersionPresence change that happened in Sitecore 8.1. Although, I'm not sure what version you upgraded from.

The EnforceVersionPresence setting makes Sitecore return null item objects instead of a shell item object with zero versions.

You have 2 options. Update your code to check that the item has versions before adding it to your model. On the item object you can do:

item.Versions.Count > 0

The second option is more of a global setting and that would be to turn EnforceVersionPresence on for your sites. To do that you will need to patch in the following config:

<sites>
  <site name="<yoursite>">
    <patch:attribute name="enforceVersionPresence">true</patch:attribute>
  </site>
</sites>

You can then enable the feature on items and/or Standard Values where you need this to happen. In the Advanced section of the item, there is a field called Enable Enforce Version Presence. Make sure this is ticked.

For more information see this kb article: https://kb.sitecore.net/articles/350702

1
  • Thank you @richardSeal. Let me try this Jun 19, 2019 at 21:10
0

A potential solution could be to ensure that the datasource is not only set (guid) but that it also exists (item or null) inside the GetModelFromView.cs. You can also then include the version check as mentioned by Richard.

This can be accomplished by changing GetModelFromView.cs

object model = null;

if (renderingItem.DataSource.HasValue())
{
    var getOptions = new GetItemByPathOptions();
    getOptions.Type = modelType;
    getOptions.Path = renderingItem.DataSource;
    model = mvcContext.SitecoreService.GetItem(getOptions);
}
else if (renderingItem.RenderingItem.DataSource.HasValue())
{
    ...
}

to check if the item is valid or continue

// Check if the datasource is set and actually exists
object item = null;
if (rendering.DataSource.HasValue())
{
    var getOptions = new GetItemByPathOptions();
    getOptions.Type = modelType;
    getOptions.Path = renderingItem.DataSource;
    item = mvcContext.SitecoreService.GetItem(getOptions);
}

if (rendering.DataSource.HasValue() && item != null)
{
    model = item;
}
else if (rendering.RenderingItem.DataSource.HasValue())
{
    ...
}

Disclaimer: This solution requires you to override Glass Mapper and could have other unforseen issues.

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.