Skip to content
the poor coder

sitecore rendering paramaters get

Poor Man
Published
Reading time
1 min read
On this page

Sitecore Rendering Parameters Get#

I have been working with Sitecore for a while now, and I have come across the need to retrieve rendering parameters from a rendering in Sitecore. Sitecore provides an API to get rendering parameters, which can be accessed using C# code.

Using Sitecore API to Get Rendering Parameters#

The first step is to get the current rendering item using Sitecore's RenderingContext class. We can then access the rendering parameters using the GetParameters() method.

C#

      var renderingItem = RenderingContext.Current.Rendering.Item;
      var renderingParameters = new RenderingParameters(renderingItem["Parameters"]);
      var parameterValue = renderingParameters["ParameterName"];
    

In the above code, we first get the rendering item using RenderingContext.Current.Rendering.Item. We then create a new instance of the RenderingParameters class by passing in the Parameters field of the rendering item. Finally, we can access individual parameters using indexing, like renderingParameters["ParameterName"].

Using Sitecore Query to Get Rendering Parameters#

We can also use Sitecore Query to get the rendering parameters. We can execute a query to get the parameters field of the current rendering item and parse it using C# code.

C#

      var renderingItem = RenderingContext.Current.Rendering.Item;
      var parametersField = renderingItem.Fields["Parameters"];
      var parameterValue = Sitecore.Web.UI.WebControls.FieldRenderer.Render(parametersField);
    

In the above code, we get the Parameters field of the current rendering item using renderingItem.Fields["Parameters"]. We then use the FieldRenderer.Render() method to render the field value as HTML. We can then parse the HTML to get the individual parameters.

Conclusion#

There are multiple ways to get rendering parameters in Sitecore, using either the Sitecore API or Sitecore Query. Depending on the requirement and the context, we can choose the appropriate way to get the parameters.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC