9

What is the best way to create many items in bulk under a certain item, using code?

What are the things to consider in such scenarios?

What are the pros and cons?

3
  • Around how much items do you intend to create? Because I normally use a batch which creates items under a specific node and this runs at a scheduled timeframe Commented Oct 12, 2016 at 20:46
  • How many items roughly and are they bucket-able?
    – vandsh
    Commented Oct 12, 2016 at 20:53
  • @HashmatAli, would you be able to provide a little more clarity into what you're asking? Commented Oct 12, 2016 at 22:48

3 Answers 3

22

I have outlined 3 options for you below. Rocks is perhaps the easiest to setup and use but isn't so easy to use on remote environments. SPE is perhaps the most flexible and powerful option. Writing code against the APIs is quick and dirty but gets the job done. So there is no right answer really, it depends on your use-case.

Option 1: Sitecore Rocks

You can use Sitecore Rocks to do this quite easily:

https://visualstudiogallery.msdn.microsoft.com/44a26c88-83a7-46f6-903c-5c59bcd3d35b/view/Discussions/3

Install Rocks from the link above and then in the Sitecore Rocks explorer, select the node/item where you want to create your items, bring up the context menu and select "Add" and then "New Item":

enter image description here

Select the template you want to use and add the details of one or more items like so:

enter image description here

Click ok and Rocks should create them for you.

Option 2: Sitecore Powershell Extension

Another option is Sitecore Powershell Extensions. Install it from here:

https://marketplace.sitecore.net/en/Modules/S/Sitecore_PowerShell_console.aspx

Add the following script and run it, it will prompt you for the item and template to use and then create items:

#This script will create sitecore items based on given count and template
$InputCount = Show-Input "How many items do you want to create?"
$InputTemplate = Show-Input "Using which template you want to create item?"
$getCurrentItem = Get-Item .
$NewItemPath = $getCurrentItem.FullPath
$CurrentChildCount = $getCurrentItem.Children.Count+1
for ($index = $CurrentChildCount; $index -lt $CurrentChildCount+$InputCount; $index++) 
{
   new-item -Path $NewItemPath -Name $index -type $InputTemplate
}

More info on how to use this script here: http://sitecorejourney.nileshthakkar.in/2015/10/how-to-create-bulk-items-in-sitecore.html

Great GIT Book on SPE here: https://sitecorepowershell.gitbooks.io/sitecore-powershell-extensions/content/

Option 3: Sitecore APIs

Write some c# code to do this and put it in a page in /sitecore/admin or somewhere else that is secure, add the Sitecore dll/namespace references needed and run it.

using (new SecurityDisabler())
{

  Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

  Item parentItem = masterDb.Items["/sitecore/content/home"];

  TemplateItem myTemplate = masterDb.GetTemplate("my template");

  for (int i = 1; i <= 100; i++)
  {
    parentItem.Add("my test item " + i, myTemplate);
  }
}

This should create 100 items for you based on your specific template under the home item. Just change the path to the parent item and the template you want to use and it should work fine. Notice SecurityDisabler is used to allow permissions to create items.

2
  • 1
    @Hashmat - if my response answered your questions please can you accept it as the answer? Commented Oct 13, 2016 at 15:39
  • It might also help to add a BulkUpdateContext around this code snippet. You may need to publish / re-index afterwards if necessary. (This is why it's so much faster initially, so depending on your situation you may not gain time overall). Summarised here: jermdavis.wordpress.com/2015/07/27/… Commented Oct 17, 2016 at 18:27
17

Sitecore PowerShell Extensions provides a great way to create items in bulk.

Here is a simple script.

foreach($index in 1..10) {
    $item = New-Item -Path "master:\content\home\sample item$($index)" -ItemType "Sample/Sample Item"
    $item.Title = "Auto-generated index $($index)"
}

Check out the Gitbook for SPE here for more details.

Pro

  • In a few lines of code using the PowerShell syntax and Sitecore API you can do all sorts of things
  • Lots of documentation, videos, and people that know the module

Con

  • Requires the Sitecore PowerShell Extensions module to be installed within the Sitecore instance
  • Requires some familiarity with PowerShell
  • Not too difficult to shoot yourself in the foot. Remove-Item can wipe out an entire tree.

Update 1

I've created a sample for generating users and items in SPE with the help of Read-Variable. Check it out here.

Demo

1
  • Corey, Thanks for formatting with syntax highlighting. For some reason I thought it was automatic. Commented Oct 20, 2016 at 2:57
9

Sitecore actually provides an OOTB item generator tool, named "FillDB."

You can access this tool by navigating to http://yoursite.com/sitecore/admin/filldb.aspx. Before you can use the tool, you will have to enable it via the EnableFillDB config setting.

Screenshot of Tool's Page

Pros

  1. Tool is OOTB and supported by Sitecore
  2. Config setting ensures access is only granted when a developer wants it to be
  3. Will auto-populate fields with Loren Ipsum or any other dictionary you provide
  4. Can tell tool to auto rebuild indexes upon completion
  5. Has cache clearing support
  6. Has several other features for fine-tuning your item generation

Cons

  1. Can forget to disable config setting upon completion
  2. SQL prep script needs to be run
  3. Does not create any special indexes of generated items to make it easier to delete them all and deleting can be dangerous and time consuming

Additional Considerations:

  1. Not all fields can be prepopulated without custom code (e.g. List fields and reference fields)
  2. You must be careful to delete all your sample items before hand-off
  3. You must be careful not to delete non-sample items
  4. The sample field values may not be meaningful enough or may not have the right format/structure for your solution depending on req's (you may be able to work around this by providing your own words dictionary)

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