I am trying to set a data source during runtime without updating the item in rendering, but am not able to achieve it. I tried the below approach, but that is updating the rendering data source, I have a list of data sources that need to be assigned based on the user type logged in.

 LayoutField layoutField = new LayoutField(item.Fields[Sitecore.FieldIDs.FinalLayoutField]);
        LayoutDefinition layout = LayoutDefinition.Parse(layoutField.Value);
        foreach (DeviceDefinition device in layout.Devices)
            if (device.Renderings != null)
                for (var i = 0; i < device.Renderings.Count; i++)
                    RenderingDefinition rendering = (RenderingDefinition)device.Renderings[i];
                    if (rendering.ItemID == "**item id goes here**")
                        //rendering.Datasource = renderingDatasource;  // creates duplicates rendering in the layout details window.   
                        rendering.DynamicProperties.Where(p => p.Name == "s:ds").ToList().ForEach(p => p.Value = "**updated datasource**");

But, in the above approach, DynamicProperties is null. Requesting help in achieving the dynamic rendering data source during runtime without updating item data source.

  • 4
    It would be a lot easier to do this if you just use a controller rendering and set the model data in the controller. – Chris Auer Apr 1 '19 at 15:02
  • Actually am using controller rendering. – Prakash Apr 2 '19 at 1:48

I think you can achieve you goal (set data source based on the user type) without a lot of customization. I am not quite sure is it applicable for you, but you can set up the personalization rules for your renderings that should have a different datasources per user type.

  1. Select an item and and open the 'Presentation Details'. Click 'Edit' for 'Shared' or 'Final Layout';
  2. Choose a rendering that you want to apply deferent datasources to and click 'Personalize' button;
  3. Click 'Add personalization rule' button:

add personalization rule

  1. Then click 'Edit Role' and find the where the current user is a member of the specific role rule:

where the current user is a member of the specific role

  1. Choose a datasource which would be set if condition is true.

So you can set a different datasources for each roles.

Also this approach keeps an ability to use Experience Editor and make it easy to manage datasources for Content Editor at all.


Personalization is a great way to go, but it seems like you are going to need some display rendering rules to know which role the user is on.

An easy way around this is to just use your controller rendering and feed the view the right data based on your rules.

public ActionResult MyControllerRendering()
    Item model;

    if (User.Type = "User1")
        model = Sitecore.Context.Database.GetItem(Constants.UserOneitem);

    if (User.Type = "User1")
        model = Sitecore.Context.Database.GetItem(Constants.UserTwoitem);

    return View(model);

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.