Vikram
Vikram

Reputation: 713

Sitecore rule does not set the data source to other item?

This is my rule

where user profile fb_likes field contains sitecore

set data source to TestItem2

I have applied this rule to a sublayout on standard values of the template,but this rule never change the data source.

I have also tried this condition

where true (action always execute).

but again no luck,

if I change action to

hide rendering

it works fine.
what I'm doing wrong here??

Upvotes: 1

Views: 711

Answers (1)

dmgdotnet
dmgdotnet

Reputation: 362

Does the code of your sublayout make allowances for using the Datasource when it is set over the context item? You can achieve this in a number of ways. E.g in a base class:

    protected string DataSource
    {
        get
        {
            var sublayout = Parent as SublayoutBase;
            return sublayout == null ? string.Empty : sublayout.DataSource;
        }
    }

    protected Item DataSourceItem
    {
        get
        {
            return string.IsNullOrEmpty(DataSource)
                       ? Sitecore.Context.Item
                       : Sitecore.Context.Database.GetItem(DataSource) ?? Sitecore.Context.Item;
        }
    }

Then inside your code for your sublayout use the DatSourceItem rather then the context item to display content. Another way I have seen this done is to:

    protected override void Render(HtmlTextWriter writer)
    {
        if (this.DataSourceItem != null)
            using (new Sitecore.Data.Items.ContextItemSwitcher(this.DataSourceItem ))
            {
                base.Render(writer);
            }
        else
        {
            base.Render(writer);
        }
    }

Using this all your sublayouts that inherit this in their base class will natively support Data source even if the code is written against the Context item.

Upvotes: 1

Related Questions