0

I need to programmatically update the Default Bucket Query in a Saved Search in Sitecore. I am fairly new to this area, how do I get this done?

  • 1
    What do you mean grammatically? Do you mean you need SiteCore to be spelled correctly Sitecore :D – Vlad Iobagiu Jul 12 '17 at 5:01
  • Sorry I meant programatically :). I have access to the SavedSearch item in SiteCore however I dont know how to access the "Default Bucket Query" field and perform the udpate. – tmp dev Jul 12 '17 at 5:17
  • 1
    Do you know item id? If you do, have you tried getting item by id from the database and changing field's value? Eg.: learnsitecore.cmsuniverse.net/Developers/Articles/2009/06/… – grg Jul 12 '17 at 14:26
  • When do you need to change this value? Just as a one off? or an event such as when the search page is loaded in the CMS? – Richard Seal Jul 12 '17 at 19:12
  • I actually need to change it on an item:updated even. The user basically creates an item off a branch template and on the creation process I need to point this saved search into the proper location. – tmp dev Jul 12 '17 at 22:03
2

Updating the field

The field you need is ID Sitecore.Buckets.Util.Constants.DefaultQuery and then if you have your query already you can just update it.

BucketItem.Editing.BeginEdit();
BucketItem.Fields[Sitecore.Buckets.Util.Constants.DefaultQuery].Value = ['The query you want in there']
BucketItem.Editing.AcceptChanges();

The query looks like this +location:021e2ec55a614a3d8b2f5575e709c632;template:{03360FC1-B4C0-4770-9E1D-79E8317B74DD}. So you can update it to whatever you want.

Doing it on an item created from a branch template

As far as your branch update goes, I would do that work in a AddFromTemplate pipeline. This gives you the ability to focus on branches and not every single time an item is updated.

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;

        [DO YOUR BRANCH WORK HERE]

    }
}

The config is like this

<!-- Add this to App_Config/Include to enable branch presets -->
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <group name="itemProvider" groupName="itemProvider">
                <pipelines>
                    <addFromTemplate>
                        <processor type="YourApp.BranchPresets.AddFromBranchPreset, YourApp.BranchPresets" />
                    </addFromTemplate>
                </pipelines>
            </group>
        </pipelines>
    </sitecore>
</configuration>
| improve this answer | |

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.