0

There is a Controller rendering and in its Data source field, I gave the path of the datasource item.

[ The datasource item's path is set in the rendering itself and not in the presentation details. ]

I can get the datasource item in code like this

string datasourceId = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull.Rendering.DataSource;

But the same is not working for a View rendering. The code returns empty value.

The View rendering is loaded from the layout's cshtml like this

@Html.Sitecore().Rendering("<path of the rendering>")

Tried with and without assigning a Sitecore model to the rendering.

Using Sitecore 8.2

1

There are two ways to get the datasource value in your case.

  1. You need to provide datasource item from your layout where you are calling the Rendering as partial as given below:

    @{
        var datasourceItem = "Guid or Path of item"
    }
    
    @Html.Sitecore().Rendering("RenderingPath", new { DataSource = datasourceItem })
    

Or if your data-source will always remain same then you need to provide/enter the datasource value into Datasource field of rendering and then by the below code you can get the value of datasource field on rendering.

RenderingContext.CurrentOrNull.Rendering.RenderingItem.DataSource

Hope this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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