Reputation: 11
I am generating a lot of sitecore content items programmatically in a tree structure. To provide an example, the structure looks something like this in content tree:
Sitecore
content
Item1
SubFolder1 (containing thousands of subitems)
Item2
SubFolder2 (containing thousands of subitems)
...and so on
Now, all the subitems within Subfolder1 use the same template, similarly other SubFolders too. I want to make all the Subfolders bucketable programmatically. I know how to do this using Sitecore UI but that's not practical in this case as there will be thousands of such subfolders. So, how do I do that programmatically?
Upvotes: 1
Views: 2469
Reputation: 2422
Add the following namespace to use buckets extension methods:
using Sitecore.Buckets.Extensions;
Use the following code to create item buckets:
public static void CovertToBucketItem(Item SubFolderItem)
{
Sitecore.Buckets.Managers.BucketManager.CreateBucket(SubFolderItem);
using (new Sitecore.Data.Items.EditContext(SubFolderItem, SecurityCheck.Disable))
{
if (!IsBucketItemCheck(SubFolderItem))
{
IsBucketItemCheckBox(SubFolderItem).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];
}
All subitems can be added to SubFolderItem
the usual way.
SubFolderItem.Add(SubItemName,SubItemTemplate)
Make sure that template's Standard Value
for Subitems have the field Bucketable
checked.
Hope this helps.
Upvotes: 4