9

I use sitecore 8.2 and I can't understand the purpose of the "Data source" field on the "View rendering" item.

It seems that datasource works only when I statically specify it inside the cshtml file:

@Html.Sitecore().Rendering("/sitecore/layout/renderings/jobs/footercontent", new {DataSource = "/sitecore/content/Global/Footer"})

But if I define the datasource in the "Data source" filed of the view rendering then it stops working.

enter image description here

@Html.Sitecore().Rendering("/sitecore/layout/renderings/jobs/footercontent")

From your experience, why could it happen? Shouldn't it work when I dynamically bind the datasource in the "Content Editor"?

5

When you specify the datasource in the Data Source field of the Rendering item definition, you effectively set the "default datasource" for that component.

If you do not specify the Data Source in Control Properties in Experience Editor mode (or Presentation Details in Content Editor) then the datasource you specified on the Rendering item definition will be used.

It's also worth noting that if you set the datasource on the Rendering item then the Select the Associated Content dialog will not appear when you add a component to a page.

But setting the "default datasource" this way is the same whether you dynamically or statically bind your components.

Since your static binding is pointing to a Sitecore Rendering definition item, it should use this datasource. It works as expected (and like in your example) in Sitecore 8.1 Update-3 but this looks like a bug in Sitecore 8.2, or at least a breaking change. I would raise a ticket with Sitecore Support and have them investigate/fix this.

1

Actually this is a bug - rename the field in the definition item of the component as Datasource (i.e. remove the space) and it will work just fine.

0

If you are setting this on the view rendering I'm not sure Sitecore will pick that up by default. I think it will use the page content item as the datasource.

I believe you need to use something like RenderingContext.Current.Rendering.Item when calling @Html.Sitecore().Rendering in order to pass the data source you have configured in Sitecore into your rendering.

0

You can do it using next code:

       @Html.Sitecore().Rendering("{240A3624-8600-4A7B-8C07-5846A673F1F0}", new
                           {
                               DataSource = "{7CB5F3A9-F301-44C0-AB73-7A19837DA01C}"
                           })

First time is checking if the Rendering has a definition item and it set a datasource if it has a definition item. After it verifies if has a Parameter in the .cshtml and if exist then it override the definition item.
Bellow code is used when you use .Rendering helper method

  protected virtual Rendering GetRendering(string renderingType, object parameters, params string[] defaultValues)
    {
        Rendering rendering = new Rendering();
        rendering.RenderingType = renderingType;
        for (int i = 0; i < defaultValues.Length - 1; i += 2)
        {
            rendering[defaultValues[i]] = defaultValues[i + 1];
        }
        if (rendering.RenderingItem != null)
        {
            Item innerItem = rendering.RenderingItem.InnerItem;
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cacheable");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_Timeout");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_VaryByData");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_VaryByDevice");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_VaryByLogin");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "CacheKey");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_VaryByParameters");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_VaryByQueryString");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Cache_VaryByUser");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "DataSource");
            this.CopyPropertyFromDefinitionItem(rendering, innerItem, "Model");
        }
        if (parameters != null)
        {
            Dictionary<string, object> properties = TypeHelper.GetProperties(parameters);
            properties.Each(delegate(KeyValuePair<string, object> pair)
            {
                rendering.Properties[pair.Key] = pair.Value.ValueOrDefault((object o) => o.ToString());
            });
        }
        return rendering;
    }

I tried on Sitecore 8.2.1 and it works as expected for me.

0

You can either use a rendering as dynamic rendering or static rendering.

If you are using rendering as static rendering then you will also need to define data source statically like in your example.

@Html.Sitecore().Rendering("/sitecore/layout/renderings/jobs/footercontent", new {DataSource = "/sitecore/content/Global/Footer"})

But if you want to supply data source through datasource field then you will also need to give placeholder name in other field and use the rendering dynamically like. @Html.Sitecore().Placeholder("MyPlaceholder")

0

When your view rendering is part of your item's presentation then I don't think so you want to explicitly mention data source in cshtml file.

You might be aware that request could follow in below sequence when view rendering is concerned

(After HttpRequstPipeline) --> sitecore item (with presentation details) --> sitecore's view rendering --> MVC area 's view rendering.

Hope it clarifies :)

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.