Glass Mapper Upgrade(5.3.15) for sitecore 9

Image
GlassMapper

In this blog, I will discuss about upgrading glass mapper from 4.x to 5.3.15 and compiling it against sitecore 9 upgraded solution.For upgrading to Glass Mapper 5.3.15, Navigate to package manager console of the project and run the following script. This package is for all sitecore 9.0 versions (9.1 version have different package to be downloaded)Install-Package Glass.Mapper.Sc.90 -Version 5.3.15[caption id="attachment_5304" align="alignnone" width="550"] GlassMapper[/caption]Note that this will upgrade the Castle.Core library to 4.2.1 version.For upgrading glass, it needs to be compiled against each version of Sitecore. Kernel and System.Web.Mvc.dll assemblies. To ensure clean upgrade, ensure that all the projects are references with the correct version of Sitecore and re add the glass NuGet packages so that glass mapper refers to correct path. If Project is set up with TDS to regenerate site core models, upgrade to Glass Mapper 5.0.15 will lead to errors and should change in the following Glass Mapper APIs used in the project with Glass Mapper Auto regenerate models feature. These steps are not required if manual glass model mapping is done on solution.

  1. Convert the controller base class from Glass Controller to Controller(.NET class)
  2. ISitecoreContext has been deprecated, we need to use IMVContext to get items/context from Sitecore. For Example, The following method has been converted from
public static string GetMediaItemPath(ISitecoreContext scContext, string id)        {            var myImage = scContext.GetItem<Media_Reference>(id);         }

to

 public static string GetMediaItemPath(ISitecoreContext scContext, string id)        {              IMvcContext mvcContext = new MvcContext();  	var myImage = mvcContext.SitecoreService.GetItem<Media_Reference>(id); }
  1. In the previous versions, ISitecoreContext was used to Cast the model, now needs to be converted to site core model by using SitecoreService,For Example the calling the method should be converted from
var eventsItem = SitecoreContext.Cast<IEventDetails>(DataSourceItem);

to

IMvcContext mvcContext = new MvcContext();var eventsItem = mvcContext.SitecoreService.GetItem<IEventDetails>(DataSourceItem);
  1. If the GetItem needs to be called in cshtml file, it needs to use Mapper.Sc.GetItemByItemOptions to retrieve the item. For example, Convert the following code from
mediaReference.MediaAlt = scContext.Cast<IImage>(glassMediaItem.SitecoreItem).Alt;

needs to be converted to

IMvcContext mvcContext = new MvcContext();mediaReference.MediaAlt = scContext.SitecoreService.GetItem<IImage>(new GetItemByItemOptions(glassMediaItem.SitecoreItem)).Alt;

  1. The context item can be resolved with GetContextItem method.

Example –

var baseEventsPage = SitecoreContext.Cast<IBase_Page>(ContextItem);

to

IMvcContext mvcContext = new MvcContext();var baseEventsPage = mvcContext.GetContextItem<IBase_Page>();

  Happy Coding.