Gabbar
Gabbar

Reputation: 4066

Sitecore dynamic data source locations

I have some components in my Sitecore install that can be added to one of any multiple placeholders on a page. The data source location of the renderings of these components can change based on what placeholder they are added to the site. I started creating a processor like

<getRenderingDatasource>
        <processor patch:after="*[@type='custom']"  type="custom" />
</getRenderingDatasource>

The class is like

public class GetDynamicDataSourceLocations : GetDatasourceLocation
    {
        public void Process(GetRenderingDatasourceArgs args)
        {
            ...            
        }
    }

I can't get the placeholder that I'm trying to attach the rendering to. Is there any way I can get the placeholder or atleast the parent where the component is being added?

Thanks

Upvotes: 2

Views: 974

Answers (1)

Ruud van Falier
Ruud van Falier

Reputation: 8877

It's a very good idea you have here, but the GetRenderingDatasourceArgs can't provide you with the data you need if you're configuring the allowed datasource locations on the placeholder.

I've searched through querystring & form variables and context items as well, but there is no reference to the placeholder available in the getRenderingDatasource pipeline.

I did come up with something that could be the solution, although it's a bit hacky.

  1. Create a processor for the getPlaceholderRenderings. The GetPlaceholderRenderingsArgs will provide you with a placeholder key.
  2. Store the key in a session variable (I don't know another way of transferring data between pipelines at this point)
  3. Retrieve the key from the session in your getRenderingDatasource processor.

This is the code I used to test it:

// Add to the getRenderingDatasource pipeline.
public class GetPlaceholderKey
{
    public void Process(GetPlaceholderRenderingsArgs args)
    {
        System.Web.HttpContext.Current.Session["Placeholder"] = args.PlaceholderKey;
    }
}

// Add to the getRenderingDatasource pipeline.
public class GetAllowedDatasources
{
    public void Process(GetRenderingDatasourceArgs args)
    {
        Debug.WriteLine(System.Web.HttpContext.Current.Session["Placeholder"]);
    }
}

This works when you add a rendering to a placeholder, but I have not tested other scenarios.
I can imagine it would not work when you set the datasource of a rendering that is already placed in the placeholder.

Upvotes: 1

Related Questions