2

I am working on some code that was written years ago and I am just trying to understand how it works. On some pages, there is the following code:

        var context = new SitecoreService(Sitecore.Context.Database.Name);
        item = context.GetItem<ISomething>(DataSourceItem);

I'm trying to determine how the value DataSourceItem is populated - it is not set by my code explicitly. A full search of my code never shows it being set; plus, it doesn't have a 'set' accessor anyway. The question is: for each rendered page, how does Glass Mapper determine the value it should be set to?

If I follow it to it's source, it's in an abstract class that appears auto-generated:

    // Summary:
    //     Returns the item specificed by the data source only. Returns null if no datasource
    //     set
    public Item DataSourceItem { get; }

If I debug the code, the value of DataSourceItem is a GUID that I can easily find in the content editor of Sitecore. Where is this variable populated? I've searched the code, Google, the Sitecore community, and elsewhere. Should be simple, right?

Edit: A couple of people are suggesting that I decompile the Glass Mapper code to see where the value of DataSourceItem is set. Also, a search of my entire codebase doesn't show that this variable is set by my code.

What I really want to know is what determines how DataSourceItem is set. This variable is being set internally by Glass Mapper, but something must have been passed in to it, instructing Glass Mapper to pull an item from Sitecore. I don't see that in the code.

4
  • Search for DLLs containing the word Glass in your website bin folder and use JustDecompile or dotPeek to decompile. Chances are that Glass Mapper is doing this somewhere in an external DLL. Also try searching for all instances of DataSourceItem in your code (or right clicking and select find all references). Commented Sep 6, 2022 at 21:27
  • Also, DataSourceItem is typically associated with the IRenderingModelBase interface. Commented Sep 6, 2022 at 21:38
  • This variable is being set internally by Glass Mapper, but something must have been passed in to it, instructing Glass Mapper to pull an item from Sitecore. I don't see that in the code.
    – T H
    Commented Sep 7, 2022 at 11:43
  • @TH is your controller is inheriting GlassController ? Commented Sep 7, 2022 at 14:37

2 Answers 2

3

If you check the DLL Glass.Mapper.Sc.Web.Mvc, you will find the implementation of this under GlassController

/// <summary>
/// Returns the item specificed by the data source only. Returns null if no datasource set
/// </summary>
[ExcludeFromCodeCoverage]
public virtual Item DataSourceItem => !this.RenderingContextWrapper.HasDataSource ? (Item) null : Sitecore.Context.Database.GetItem(this.RenderingContextWrapper.GetDataSource());

And if you check the method GetDataSource(), you will find it's implementation.

    public string GetDataSource()
    {
      string dataSource = RenderingContext.CurrentOrNull.Rendering.DataSource;
      if (dataSource.IsNullOrWhiteSpace() && RenderingContext.Current.Rendering.Item != null)
        dataSource = RenderingContext.Current.Rendering.Item.ID.ToString();
      return dataSource;
    }

The above implementation is getting the rendering datasource.

Hope this makes sense.

Thanks

4
  • Thanks for your reply. Do you know how I determine what is setting DataSourceItem? My code is not explicitly setting it. Familiarity with Glass Mapper is key to understanding this, I believe as it isn't a straight C# knowledge answer.
    – T H
    Commented Sep 7, 2022 at 14:05
  • If your controller is derived from GlassController it will give you the filled values. @TH Commented Sep 7, 2022 at 14:11
  • The web app is Asp.NET WebForm (not MVC) and is derived from GlassUserControl<Item>.
    – T H
    Commented Sep 7, 2022 at 17:50
  • @TH Okay, So can you let me know that which DLL is referring to GlassUserControl. By using F12/Go To Definition. So I can verify that. Commented Sep 8, 2022 at 5:12
0

I checked into the code about this property.

public Item DataSourceItem { get; }

Above property returns the RenderingContext.CurrentOrNull.Rendering.DataSource as an item.

You can find more details in Glass.Mapper.Sc.Mvc.dll code. You can use dotPeek to decompile code.

Hope this helps.

1
  • I see that it is coming from Glass Mapper, but the DataSourceItem is different depending on the page being rendered. I cannot figure out how Glass Mapper knows which Sitecore item it needs to pull data from, depending on the Asp.NET web page being rendered.
    – T H
    Commented Sep 7, 2022 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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