Get Sitecore Item from GlassMapper’s Object

This may sound quite wired actually that one would extract the Sitecore item from the Glass Mapper’s model,but yes we did that recently.
This is tiny piece of code to show how we can convert from the GlassMapper’s object (for example from the SitecoreContext.Model object) to Sitecore item.


Option #1 Using the GlassMapper’s GetItem method:

We use this method to convert our Model into different-different GlassMapper Models,such as :
If we want to convert the Model into Products Model:

var productModel=SitecoreContext.GetItem<IProduct>(Model.Id);

So we can use this method to convert the model into Sitecore item as well:
var item=SitecoreContext.GetItem<Item>(Model.Id);

Caution:we should cache the SitecoreContext.

Option #2 The traditional way:

var item=Sitecore.Context.Database.GetItem(new Guid(Model.Id));

 

Hope this will be useful.
Credits to Akshay Sura & Nilesh Thakkar for all the help.

 

Advertisements