1
votes

I have an MVC View rendering in Sitecore 7.5 defined as:

Item Name: ViewRenderingA
Item Template: View Rendering
Path: /path/to/ViewRenderingA.cshtml
Data Source: {3AD25922-FE0E-45B3-9F2F-7EF61F678E45} // Item Id of the image in the media library

As you can see, this view rendering has its data source field set to the image item's GUID in the media library. Basically, I am trying to render an image from the media library in a particular static spot of the content item layout.

I statically pull this view rendering in the layout file as:

@Html.Sitecore().Rendering("/path/to/ViewRenderingA")

And then in the Razor View implementation ViewRenderingA.cshtml file, I do this:

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@model RenderingModel

<p>Page Item Name: @Model.PageItem.Name</p> @* <- This correctly shows the content item name. *@
<p>Rendering Name: @Model.Rendering.Item.Name</p> @* <- Why does this also show the content item name instead of the item name of the image in the media library that's linked to the View rendering via data source? *@

I would expect that the Model.Rendering.Item will be initialized with the media library item that the View Rendering's "Data source" field is pointing to, but it is initialized with the Model.PageItem instead. It looks like it completely ignores the datasource field. What am I doing wrong?

1
What if you try to access the item with RenderingContext.Current.Rendering.Item? Does it work when you add the datasource statically as a parameter to the binding like described in my blog post ctor.io/…?Kevin Brechbühl
RenderingContext.Current.Rendering.Item also points to the content item (I think it's called "context item") as the other 2. It only works if I add the data source as a parameter like this: @Html.Sitecore().Rendering("/path/to/ViewRenderingA", new { DataSource = "/path/to/datasource-item" }). Looks like setting the "Data source" filed of the View rendering itself makes no difference.demisx
Hm then I would probably add a new issue on support.sitecore.net.Kevin Brechbühl
If you set the datasource on the View rendering then I believe you can use: RenderingContext.Current.Rendering.RenderingItem.InnerItem. Not sure though, its been a while since I've used this.RvanDalen
Under normal circumstances RenderingContext.Current.Rendering.Item will work for getting the current renderings datasource.Jason Horne

1 Answers

-1
votes

If you have given data source then you can access the item by using Sitecore.Context.Item

Item datasource = Sitecore.Context.Item;

And if your data source is not going to change then one workaround would be access it using its Item ID as:

dataSource = Sitecore.Context.Database.GetItem(ItemId);

In your case Item Id would be ID of your media library content I hope this helps.