6

My goal is to automatically set the datasource of a rendering when an certain rendering is added to the page in the Experience Editor. Since the datasource item is created when the user adds a rendering, I cannot set a static datasource path on the rendering itself.

Currently I have the following partial solution:

public void Process(GetRenderingDatasourceArgs args)
    {
        if (args == null)
        {
            throw new ArgumentNullException(nameof(args));
        }

       // ... some code to: 
       // - detect whether a datasource item should be created
       // - create datasource item
       // ...

       // set CurrentDatasource to the path of the newly added datasource item        
       args.CurrentDatasource = datasourceItem.Paths.FullPath;

        }
    }

This pipeline is patched before: Sitecore.Pipelines.GetRenderingDatasource.CheckDialogState, Sitecore.Kernel

This results in the datasource item being created correctly and prefilled in the "Select the Associated Content"-dialog. However, I would like to skip this dialog and set the datasource of the rendering on the page immediately to the item I just created. Any suggestions how I can achieve this?

The dialog:

Select the Associated Content dialog

3

You need to overwrite (create a new) AddRendering command. As you can read here the dialog is opened in that command and the only way to really change the logic there is create you own. As shown in the blog, it can be done but be careful when upgrading as you need to "copy" some Sitecore code.

The trick is to send a WebEditResponse with the correct parameters:

WebEditResponse.Eval(FormattableString.Invariant(
    $"Sitecore.PageModes.ChromeManager.handleMessage('chrome:placeholder:controladded', 
          {{ id: '{itemNotNull.ID.Guid.ToString("N").ToUpperInvariant()}', 
             openProperties: {flag.ToString().ToLowerInvariant()}, 
             dataSource: '{datasourceItem.ID.Guid.ToString("B").ToUpperInvariant()}' }});"));
3

The "No Code" approach

You can control the dialogue that pops up after adding a rendering. Navigate to your rendering and find the "Open Properties after Add" field. Set it to "No".

Open Properties after Add

Sitecore should not be popping up any property dialogues with this set.

If you want to avoid Sitecore popping the "Select Associated Content" dialogue, the only obvious way to do it is to not tell Sitecore about the expected template type for the rendering. E.g. clearing the "Datasource Template" field.

Datasource Template

A bit of code

You're on the right track with your code. You're patching in before the CheckDialogState processor. A deeper look at how it works:

public void Process(GetRenderingDatasourceArgs args)
{
  Assert.IsNotNull((object) args, "args");
  string str = args.RenderingItem["data source"];
  bool flag = !string.IsNullOrEmpty(str);
  if (string.IsNullOrEmpty(args.CurrentDatasource) && !this.IsCurrentRenderingContextItem(args))
    args.CurrentDatasource = str;
  if ((!flag || args.ShowDialogIfDatasourceSetOnRenderingItem) && args.DatasourceRoots.Count != 0)
    return;
  args.AbortPipeline();
}

The thing to note here is, that it calls args.AbortPipeline() if it decides, no dialogue should be shown. I think you need to add that to your code snippet above, after having set the datasource you require on args.CurrentDatasource.

  • Unfortunately that is not the dialog I meant. I added a screenshot of the dialog to the question in order to clarify. – Nico Grms Jan 16 '17 at 16:09
  • I missed that. Expanded my answer. – Mark Cassidy Jan 16 '17 at 16:20
  • I have tried that, however if I abort the pipeline after setting args.CurrentDatasource, no datasource is set. It seems like args.CurrentDatasource is only used to preselect the datasource item in the "Select the Associated Content"-dialog. – Nico Grms Jan 16 '17 at 16:52
  • It's never easy, is it ;-) how about setting args.DialogUrl to string.Empty before the abort() call? – Mark Cassidy Jan 16 '17 at 17:05

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.