5

I have a branch template in Sitecore with a bucket. Inside the bucket I want to include two saved searches that are available to users as soon as they create an instance of the branch. The problem is that the query for the saved search contains something like the following

custom:IsActive|true;template:{d9577b16-776f-48c3-b2f8-e31514acd492};location:{f0401dc9-d076-4a3c-b9c5-5ac42bc25d48}

My problem is the location filter. The id refers to the branch template location which is fine for the moment, but when an instance of the branch is created I want it to point to the actual location where the item was created. How do I achieve this? do I need to write custom code?

1 Answer 1

3

I don't know much about bucket location filters, but it looks to be a field on the bucket item. Where you want to start is with the addFromTemplate pipeline. This is the pipeline that is called when you create an item from a branch. Once you access this pipeline you can gain access to the branch items after it is created.

I used code from Kamsar's Branch Presets project to update my layout datasource paths after creating an item from a branch. This same code can be used in your case. You just want to update Id_Of_Your_Bucket_Item with the template of you bucket item or some other way of finding it after it was created. And update your query to whatever you need it to be.

The code

public class AddFromBranchPreset : AddFromTemplateProcessor
{
    public override void Process(AddFromTemplateArgs args)
    {
        Assert.ArgumentNotNull(args, nameof(args));

        if (args.Destination.Database.Name != "master") return;

        var templateItem = args.Destination.Database.GetItem(args.TemplateId);

        Assert.IsNotNull(templateItem, "Template did not exist!");

        // if this isn't a branch template, we can use the stock behavior
        if (templateItem.TemplateID != TemplateIDs.BranchTemplate) return;

        Assert.HasAccess((args.Destination.Access.CanCreate() ? 1 : 0) != 0, "AddFromTemplate - Add access required (destination: {0}, template: {1})", args.Destination.ID, args.TemplateId);

        // Create the branch template instance - analogous to base.AddFromTemplate(...)
        var newItem = args.Destination.Database.Engines.DataEngine.AddFromTemplate(args.ItemName, args.TemplateId, args.Destination, args.NewId);

        // find all rendering data sources on the branch root item that point to an item under the branch template,
        // and repoint them to the equivalent subitem under the branch instance
        UpdateBucketLocationFilter(newItem, templateItem);

        args.Result = newItem;
    }

    protected virtual void UpdateBucketLocationFilter(Item item, BranchItem branchTemplateItem)
    {
        var bucketTemplate = new TemplateID(new ID("Id_Of_Your_Bucket_Item"));

        var bucketItem = item.Children.FirstOrDefault(x => x.TemplateID == bucketTemplate);

        if (bucketItem == null) return; 

        bucketItem.Editing.BeginEdit();
        bucketItem.Fields[Sitecore.Buckets.Util.Constants.DefaultQuery].Value = "your query";
        bucketItem.Editing.AcceptChanges();
        bucketItem.Editing.EndEdit();
    }
}

The patch file

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <group name="itemProvider" groupName="itemProvider">
                <pipelines>
                    <addFromTemplate>
                        <processor type="YourApp.BranchTemplates.AddFromBranchPreset, YourApp.BranchTemplates" />
                    </addFromTemplate>
                </pipelines>
            </group>
        </pipelines>
    </sitecore>
</configuration>
2
  • thanks for this, should I use this or should I use the Item:Saved event. I guess I should use the pipeline, however just to confirm, and can you give me a scenario where I should use events instead of pipelines?
    – tmp dev
    Commented Jul 6, 2017 at 5:06
  • 1
    Great answer! +1! Just one clarification: the addFromTemplate pipeline is called when you add an item from a template or branch and is called whether or not you add from the UI. Commented Jul 6, 2017 at 15:09

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.