3

I got a scenario where I have different layouts Layout1, Layout2, so on...I have a renderings which is shared among this layouts. Layout1- Rendering -R1 Layout2-Rendering-R1 and so.

Now I have situation where I want to identify the layout name or details within this rendering R1. I know this is not good idea still is there any way to refer the details of layout within renderings.

  • By "different layouts", do you mean that you have a number of template fields that have presentation settings in them? Or do you mean you have the same rendering in multiple placeholders? – Richard Szalay Nov 24 '16 at 4:26
  • Actually a Layout in presentation not fields. In mvc way, i got layout and partial view. Is there a way i can check the layout details as in say name or something within partial view. – Santosh Poojari Nov 24 '16 at 4:34
  • Are you just wanting to know the layout for the page? – Chris Auer Nov 24 '16 at 5:02
6

If I understood your question right, you want to handle a particular rendering R1 based on the underlying Sitecore layout selected for the page item

The below screenshot shows how to get the layout information from the current rendering:

enter image description here

Here's a code sample that gets the current layout name:

Guid layoutId = Sitecore.Mvc.Presentation.RenderingContext.CurrentOrNull.Rendering.LayoutId;
Item item = Context.Database.GetItem(new ID(layoutId));
string layoutName = item["Name"];
| improve this answer | |
  • 1
    Could you add a code sample for accessing current layout details (such as its name)? – Dmytro Shevchenko Nov 24 '16 at 7:51
  • From Sitecore perspective LayoutId is just a GUID and we can use it to retrieve the LayoutItem using Sitecore Data API and pull its fields. John West blog gives multiple ways of accessing it. – phani Nov 24 '16 at 8:05
  • In my opinion, without a code sample, your answer is incomplete. – Dmytro Shevchenko Nov 24 '16 at 8:10
  • @DmytroShevchenko here you go :) Sitecore.Data.Items.Item item = context.GetItem(layoutId); var fieldValue = item["FieldName"]; – phani Nov 24 '16 at 9:39
7

You can access the layout in your code by using Sitecore.Context.Item.Visualization.Layout (example for context item). This will give you a LayoutItem that gives you access to the name, physical file, ...

| improve this answer | |
  • @DmytroShevchenko - The above code gives the Layout item without the need to use Data API. – phani Nov 24 '16 at 9:43

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.