13

Another question here (Can I change the order of components in placeholder based on personalization?) got me thinking - maybe there was a way to actually dynamically assign a placeholder to a rendering through personalization.

And a bit of digging - turns out, maybe there is.

Conditional Rendering Actions

So there seem to be baked-in Conditional Rendering actions for everything one could wish for - the problem is the GUI. It basically only allows for us to select the conditions (if date has passed, if xDB is enabled and so on) - and then select "Associated Content" for the rendering. Presumably this is the "Set Data Source" action behind the scenes.

Any ideas as to how we can reach the other actions? or will we just have to wait for an UI update?

2 Answers 2

11

When you set a conditional rendering, the data for the condition and action is stored inside the Layout. Here is an example of a rendering with an "Alternative Datasource" rule that is applied if the current date is Wednesday:

<r xmlns:p="p" xmlns:s="s" p:p="1">
    <d id="{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}">
        <r uid="{B343725A-3A93-446E-A9C8-3A2CBD3DB489}">
            <rls>
                <ruleset>
                    <rule uid="{EB08864D-C3BA-4130-A22B-ECF765D368E5}" s:name="Alternative Datasource">
                        <actions>
                            <action uid="D3AF4C7A8CF14BC59601B2E88CE2ECFC" s:id="{0F3C6BEC-E56B-4875-93D7-2846A75881D2}" s:DataSource="{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}" />
                        </actions>
                        <conditions>
                            <condition uid="86296CD54FE94F96B27C153C3318B5C7" s:id="{1F15625B-8BDC-4FD2-8F0C-6EE2B8EF0389}" s:day="{8A19F2AA-ABB9-4496-A64E-56CDBE1C2D4C}" />
                        </conditions>
                    </rule>
                    <rule uid="{00000000-0000-0000-0000-000000000000}" s:name="Default">
                        <conditions>
                            <condition uid="CE7C9CE0A8534636BCD0DCC373A8B661" s:id="{4888ABBB-F17D-4485-B14B-842413F88732}" />
                        </conditions>
                        <actions />
                    </rule>
                </ruleset>
            </rls>
        </r>
    </d>
</r>

Your intuition is right that this is connected to the "Set Data Source" action behind the scenes. The reference can be spotted here:

<action uid="D3AF4C7A8CF14BC59601B2E88CE2ECFC" s:id="{0F3C6BEC-E56B-4875-93D7-2846A75881D2}" s:DataSource="{0DE95AE4-41AB-4D01-9EB0-67441B7C2450}" />

s:id represents the ID of the Action Item ("Set Data Source")

s:DataSource represents the value of the parameter DataSource to be passed to the action.

Both of these parameters can be changed. For example to change the action so that it instead uses the "Set Placeholder" action and on execution sets the Placeholder value to "sidebar", you would use this:

<action uid="D3AF4C7A8CF14BC59601B2E88CE2ECFC" s:id="{9F2C4C80-8472-4296-9D41-D42A383B90DD}" s:Placeholder="sidebar" />

If this is saved and run on the published site, it absolutely works. However, there are two large drawbacks it seems, one of which you've spotted:

  • No UI support. The rule will still show in the UI and conditions on it can be edited without affecting the altered action, but there is otherwise no indication there is a special rule in place.
  • Previewing the rules in Editing mode do not appear to work. If you use the drop-down to view the alternate rule, it won't have any visible effect. This may just apply to the "Set Placeholder" action, however.

If UI support is not a problem for a solution then this could be useable.

Note that the tests for the above were performed in Sitecore 8.2 Initial Release.

6

It's possible to utilise Global Rules in order to set the placeholders, the Global Rules however are essentially "Condition and Action" items which would allow you to then use a "where CONDITION X is met, set the Placeholder to Y".

There is more details on the specifics on this blog post: Changing placeholders with Rules

The downside of this is that the Global Rules will run for all Sublayouts, so the Rules need to be more fairly generic.

It is also possible to define some "Condition and Action" rules under /sitecore/system/Marketing Control Panel/Personalization and then apply them to specific Sublayouts by enabling the rule under the Personalization section of the Control Properties dialog.

Note that I have specifically stated Sublayouts. When MVC was introduced in Sitecore the Global Rules where not run and the Personalization section does not apply to the any of the MVC rendering types.

So without a change to the interface there is no-out-of-the-box solution to currently apply that action.

3
  • 1
    Global Rules is not supported in MVC as I recall, right?
    – Mark Cassidy
    Commented Sep 14, 2016 at 10:27
  • 1
    Yes, that's what I said (although, I could have been clearer about it). Still works for WebForms though!
    – jammykam
    Commented Sep 14, 2016 at 10:31
  • D'oh, you did indeed - sorry ;-)
    – Mark Cassidy
    Commented Sep 14, 2016 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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