Convert item to bucket item programmatically

Item buckets lets you store millions of items in a repository without having a direct parent-child relationship, which solves the problem of having large number of items in content tree.

Any item in sitecore can be converted into item bucket from Content Editor, by going to 'Configure' tab then clicking 'Bucket' to convert the item into item bucket.

I was able to write some helper methods to conver item to Item bucket programmatically , in case you were importing large number of data and you wanted to covert items into buckets on the fly.

    public static void ConvertToBucketItem(Item item)
    {
        using (new SecurityDisabler())
        {   
            Sitecore.Buckets.Managers.BucketManager.CreateBucket(item);
            using (new Sitecore.Data.Items.EditContext(item, SecurityCheck.Disable))
            {
                if (!IsBucketItemCheck(item))
                {
                    IsBucketItemCheckBox(item).Checked = true;
                }
            }
        }
    }

    public static bool IsBucketItemCheck( Item item)
    {
        return (((item != null) && (item.Fields[Sitecore.Buckets.Util.Constants.IsBucket] != null)) && item.Fields[Sitecore.Buckets.Util.Constants.IsBucket].Value.Equals("1"));
    }

    public static CheckboxField IsBucketItemCheckBox( Item item)
    {
        return item.Fields[Sitecore.Buckets.Util.Constants.IsBucket];
    }
Hope this help you!

Comments

Popular posts from this blog

Attaching a file on WFFM Send email action

Building Sitecore Content Search Queryable using Rules engine

Solr with Sitecore Checklist