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?
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?
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:
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":
Select the template you want to use and add the details of one or more items like so:
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.
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.
Remove-Item
can wipe out an entire tree.I've created a sample for generating users and items in SPE with the help of Read-Variable
. Check it out here.
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.
Pros
Cons
Additional Considerations: