0

We have a branch template that creates multi-level items, i.e., a datasource item and child items (data items). The first-level item has a multi-list with the source set to the child items.

Is there any way to assign the __Standard Values to default child items?

2

3 Answers 3

1

In order to achieve, you will need to attach listener to item:create event as below, to update links of broken references in the multilist field.

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">
  <sitecore>
      <events>
          <event name="item:added">
              <handler type="UpdateLinks.Events.FixBrokenLinks,UpdateLinks" method="OnItemAdded">
                  <param desc="async">false</param>
                  <param desc="fieldIds">{CBAC939A-B8F5-4FF8-A1CA-9DBDE163906D}</param>
              </handler>
          </event>
      </events>
  </sitecore>
</configuration>

Inside the codebeside for this class, source of the branch item and item created is passed to the reference replacement job, that can be started asynchronously.

public class FixBrokenLinks
    {
        private readonly bool _async;
        private readonly string _fieldIds;
        public FixBrokenLinks(string async, string fieldIds)
        {
            this._async = string.Equals(async, "true", StringComparison.OrdinalIgnoreCase);
            this._fieldIds = fieldIds;
        }
        public void OnItemAdded(object sender, EventArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            var contentItem = Event.ExtractParameter(args, 0) as Item;
            Assert.IsNotNull(contentItem, "targetItem");

            var branchItem = contentItem.Branch;
            if (branchItem == null)
            {
                return;
            }

            var item = branchItem.InnerItem;
            Assert.IsTrue(item.Children.Count == 1, "branch item structure is corrupted: {0}".FormatWith(AuditFormatter.FormatItem(item)));

            var branch = item.Children[0];
            ID[] fields = this._fieldIds.Split(',').Select(x => new ID(x)).ToArray();

            var replacementJob = new ItemReferencesReplacementJob(branch, contentItem, true, fields);
            if (this._async)
            {
                replacementJob.StartAsync();
            }
            else
            {
                replacementJob.Start();
            }
        }
    }

Using this module, when you create items from branch templates, references of the item on the multilist fields get updated automatically.

Note: In the above configuration, you will need to pass the list of multilist field ids to make sure job only checks the fields that contain broken links.

I have uploaded the module here:

https://github.com/chiragkhanna28/BranchTemplateUpdateLinks/

Let me know in case you face any issues. Hope this helps!!!

1
  • @hariprasadkotha I have uploaded the module to work with Sitecore 9/10. Let me know in case you face any issue
    – ckhanna
    Commented May 17 at 16:38
0

You can try achieving it using Page Branch in SXA. You can create a page branch and then add the datasource item as a child and then select the default values of your mulilist fields.

You can allow the content author to create pages using page branch where they will get the page with pre-defined renderings and their data source with pre-selected values in it.

So once you have created a page branch, editors can use them as templates to create and modify pages.

To know more about page branch you can refer to these documents.

https://doc.sitecore.com/xp/en/developers/sxa/101/sitecore-experience-accelerator/page-branches.html

https://www.getfishtank.com/blog/how-to-configure-sxa-page-branches-in-sitecore

Hope it helps.

0

This functionality is not available OOTB. You can write a below powershell script and run it at the root level. Or else bind this code for a single item at the item:create event.

$items = Get-ChildItem -Path "master:/sitecore/content/" -Recurse
    
foreach($item in $items){
    $fields = $item.Fields | Where-Object { $_.Key -notlike '__**' }
    foreach($field in $fields){
        if($field.Type -eq "Multilist"){
            $childitems = Get-ChildItem -Path $item

            $dstValArr = New-Object System.Collections.ArrayList
            foreach($childitem in $childitems){
                $dstValArr.Add($dst.Id)
            }
            $dstVal = $dstValArr -join "|"

            $item.Editing.BeginEdit()
            $item.Fields[$field.Name].Value = $dstVal
            $item.Editing.EndEdit()
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.