0

We are looking for a Sitecore PowerShell script using which we can assemble/add components directly to an SXA page. I am able to add an SXA page, then add a Container component (under the page structure) to the page. But now I am not able to understand how to add any component under this Container Component via the same script.

$newSitecorePageItem = New-Item -Path $($rootItem.ItemPath) -Name "Demo 1" -ItemType "/sitecore/templates/Project/Website/Page"

$renderingPath = "/sitecore/layout/Renderings/Feature/Experience Accelerator/Page Structure/Container"

$renderingItem = Get-Item -Database "master" -Path $renderingPath | New-Rendering -Placeholder "main"

Add-Rendering -Item $newSitecorePageItem -PlaceHolder "main" -Instance $renderingItem -Parameter @{ "Reset Caching Options" = "1" } -FinalLayout:$useFinalLayout

Now suppose I want to add a Splitter Row inside this container and then further add other components into individual rows of Splitter Row i.e Component A to row1 and Component B to row2. What placeholder should I write for these components? Please suggest.

Thanks & Many Regards, Lalit Joshi

2
  • Hi Lalit. I am not sure if this will be possible because the placeholders that you are referring to are actually dynamic placeholders. Sitecore Slack should be a better platform to discuss this. Use this link to join Sitecore Slack - sitecore.chat. Mar 22, 2022 at 17:47
  • @VinayJadav it is possible as you can set/get keys for dynamic placeholders from rendering parameters of your placeholder container (splitter, container) Mar 23, 2022 at 19:02

1 Answer 1

1

First you need to understand how dynamic placeholders are generated in SXA.

To learn about it open any view with rendering supporting dynamic placeholders: Views\Container\Container.cshtml

You will notice that placeholder key has following format:

const string placeholderKeyPrefix = "container";
var key = string.Format("{0}-{1}", placeholderKeyPrefix, Model.Id);

Which can generate following placeholders: container-1, container-2

Value for Model.Id is taken from rendering parameters (param: DynamicPlaceholderId). This parameters is autogenerated when you drop a rendering on a page. It will start with value 1 or more if there is already rendering of the same type with the same index in rendering parameters.

Now, in your case when you add rendering using this line:

Add-Rendering -Item $newSitecorePageItem -PlaceHolder "main" -Instance $renderingItem -Parameter @{ "Reset Caching Options" = "1" } -FinalLayout:$useFinalLayout

you have to add DynamicPlaceholderId manually.

You have to do it because you specified parameters explicitly. If you will leave it empty SXA will autogenerate DynamicPlaceholderId (and defaults for other parameters) for you - but then you have to fetch it anyway because you cannot be sure what's the index used in placeholder.

enter image description here

p.s. I am not sure why you are trying to do it this way. Can you describe the challenge you are trying to address? Maybe there are other better ways of solving it.?

5
  • Hello @alan-płócieniak, Thank you for the quick response. We have 6 source systems and too many layouts or in other words there is too much of variation in the number components of we put on the page for one particular template which has 5k items created out of it. Moreover, we need to create page from scratch with content migration under these circumstances and client want the space to add/edit/remove components directly on the page as it is not feasible to create multiple Partial Layout for same layout structure having varying number of components in each item. Thanks, Lalit Joshi Mar 24, 2022 at 8:35
  • Also, I want to understand is there anway I can read the placeholder inside the "Container Component" or "Splitter(Row) Component" using Powershell. If not then what is the way to add dynamicid manually? thanks, Lalit Joshi Mar 24, 2022 at 8:36
  • If you want to add it manually simply define it as rendering parameter like you did here -Parameter @{ "Reset Caching Options" = "1" }. But remember to take care of uniqueness. If there will be 2x Container with the same ID you can expect problems. Mar 24, 2022 at 10:50
  • Hello @alan-płócieniak, I am talking about the "placeholder" field and now the dynamic placeholder. "/main/container-1/row-6-2/column-4-12" if you see above, the hierarchy of the item is Container -> Splitter Row -> 6th Row -> Splitter Column -> 4th Column -> My Component from which value of placeholder field is copied. I want to figure out how to put components in this hierarchy? how should I fetch the placeholder value. How can I add additional rows in the splitter row component using PowerShell. Thanks, Lalit Joshi Mar 24, 2022 at 11:07
  • Hello @alan-płócieniak, Thank You so much for the wonderful explanation. Everything is sorted now. Mar 24, 2022 at 12:34

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.