Thursday, March 27, 2014

Sitecore Parameter Templates: Invalid cast exception

Sitecore Parameter Templates

Lots of blog posts have already been written about this subject and it's not the main topic for this one, but a small introduction could be useful.
If you work with Sitecore, you probably use sublayouts. Some of those sublayouts probably need parameters. One way to get these parameters is to let the editors provide them when they attach the sublayout to the presentation of an item. It's a very flexible way to work, but before the "Parameter Templates" it was quite difficult to get a content author to enter the correct configurations for a sublayout in the "Additional Parameters" property.

Creating and using a Parameter Template

Actually it is quite simple:
  1. Create a template based upon the "Standard Rendering Parameters" (/sitecore/templates/System/Layout/Rendering Parameters/Standard Rendering Parameters)
  2. Add your parameters to this template using regular Sitecore fields types
  3. Select the template in the "Parameters Template" field of your Sublayout item (Editor Options section) 

API
Reading the values in your code can be done the same way as with the Additional Parameters (they are stored in the same way): [example in WebForms]

var sublayout = Parent as Sublayout;
var parameters = Sitecore.Web.WebUtil.ParseUrlParameters(sublayout.Parameters);
var pageIndex = parameters["PageIndex"];

Note that all values are strings. If your parameter value was an item, you will get the guid.


Result
The result in Sitecore will look like this:
 The Parameter template here had a section "NewsBox" and 2 fields "PageIndex" and "Count".


Compatibility with "Additional Parameters"
If you had additional parameters and you create a parameter template that uses the same keys as already used in those additional parameters, the data will be automatically shown in the parameter template fields (and no longer in the additional fields).

If you remove a parameter template from a sublayout the values entered on items using the sublayout with the parameters will not be deleted: they will still be shown in the additional parameters.


Invalid cast from 'System.String' to 'System.Web.UI.Page'



What I actually wanted to add in this post to the already existing ones out there is the above error.
You can get this with the use of sublayout parameters (template version or additional).
All you have to do is call your parameter key "Page".

Solution: do not call your parameter key 'Page'.. ;)

No comments:

Post a Comment