Sitecore glassmapper how to fetch the datasource from the rendering

glass mapper rendering parameters
get datasource item in sitecore mvc view rendering
glass mapper mvc context
sitecore set datasource programmatically

need an suggestion on Sitecore rendering where i want to use glassmodel to render the item.

I have a page and that has many renderings and each renderings has data source associated with it.

I know that below statement will give me the Current context, not the datasource item.

var context = new SitecoreContext();
            Model = context.GetCurrentItem<HomePage>();

What is the best option to solve my query? I gave gone through this article. But i'm really not convinced with that method as i'm using IoC (Windsor Castle) and have to write Unit test cases for each method. I may have to mock these objects later. I'm looking for the approach where i will be using Interface not the class.

Appreciate your help.

If your controller is derived from GlassController you can use GetDataSourceItem<I..>(). That will give you the datasource.

Accessing the Data Source of a Control, Controller: public class RotatorController : SitecoreController { public ActionResult Index() { var dataSourceId = RenderingContext.CurrentOrNull.​Rendering. To map list data from Sitecore in your controllers, just make the controller inherit from GlassController<T>, where T is the type of data source that your rendering uses (e.g., IProduct). You can then access the DataSource property to get your data from Sitecore.

Adding to Gatogorodo's answer

if you controller is derived from GlassController or if you are in GlassView you can use this.DatasourceItem to get the data source.

this.GetDataSourceItem<Model>() will get you the desired model which might be one of the base template for the item template.

Get Datasource Item in Sitecore MVC Controller Rendering , I know that most of the Sitecore developer's uses Glass Mapper and so they inherits the SitecoreContext class to get the data source item but  I have a small question regarding to accessing a Datasource in a view rendering. I did the Sitecore Developer course, and on one of its questions it said the following: Questions: How can you retrieve the Datasource in a view rendering with the default model? Possible Answers: - Model.Item - Model.Rendering.Item

If you using Glass Mapper 5, the documentation doesn't refer to using GlassController anymore. You can use IMvcContext like below:

using System.Web.Mvc;
using Glass.Mapper.Sc.Web.Mvc;

public class TitlesController : Controller
{
    public ActionResult Index()
    {
        IMvcContext mvcContext = new MvcContext();

        // will return the DatasourceItem if set, otherwise the page context is returned
        Titles model = mvcContext.GetRenderingItem<Titles>();

        Titles dsModel = mvcContext.GetDataSourceItem<Titles>();

        return View(model);
    }
}

Retrieve Datasource from a Controller Rendering, With the SitecreContext being the Glass.Mapper.Sc.ISitecoreContext The GetCurrentItem in the controller will always return the Context Item, not  if you controller is derived from GlassController or if you are in GlassView you can use this.DatasourceItem to get the data source. this.GetDataSourceItem<Model> () will get you the desired model which might be one of the base template for the item template.

Multiple items in a data source, You can specify a query as the data source of an MVC control. Data Source property of a control as query: text: Sample Item* to retrieve all of these items. Index() { string result = ""; foreach (Item item in RenderingContext. Glass Mapper is a must-have for every Sitecore solution. It's easy to implement, intuitive to use, and flexible to extend. Using Glass we are able to write clean, testable code efficiently and effectively.

Get data source of sublayout or rendering in Sitecore, After getting list of sublayouts, iterate through each sublayout and retrieve data source value by using rendering.Settings.DataSource. Below  # If set to true, the script will only list the renderings that need fixing, rather than fixing them.

Glass Mapper - Mapping Sitecore List Fields, Learn how to use Glass Mapper to map Sitecore list fields to your . T is the type of data source that your rendering uses (e.g., IProduct ). In the previous post we talked about What Glass Mapper is and How to Use it. In this post I will talk about What’s New and Changed in Glass Mapper 5. You can find a demo solution here. This series on Glass Mapper has been presented at the Atlanta Sitecore User Group and SUG Chennai by me and my colleague Courtney Dean. Major Changes at a Glance

Comments
  • This exactly what i was looking for. Thanks @Gatogordo