Skip to content

Instantly share code, notes, and snippets.

@nickwesselman
Last active June 12, 2019 16:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickwesselman/36125fdace3c044fff7ca0d74ea067cf to your computer and use it in GitHub Desktop.
Save nickwesselman/36125fdace3c044fff7ca0d74ea067cf to your computer and use it in GitHub Desktop.
Generate Random Bucket Content for Sitecore with PowerShell Extensions and RandomText.me
$ErrorActionPreference = "Stop"
Function Get-Lorem($Minimum, $Maximum) {
# don't overrun randomtext.me
Start-Sleep -Milliseconds 300
$words = Get-Random -Minimum $Minimum -Maximum ($Maximum+1)
return ([xml](ConvertFrom-Json (Invoke-WebRequest -UseBasicParsing http://www.randomtext.me/api/lorem/h1/$words).Content).text_out).h1
}
Function Get-LoremUl($NumItems, $Minimum, $Maximum) {
# don't overrun randomtext.me
Start-Sleep -Milliseconds 300
return (ConvertFrom-Json (Invoke-WebRequest -UseBasicParsing http://www.randomtext.me/api/lorem/ul-$NumItems/$Minimum-$Maximum).Content).text_out
}
$num = 20
$productsRoot = Get-Item "master:\sitecore\content\Basic Company\Home\Products"
$templateId = "{FA3971F5-5A1B-4477-B64E-CA99949E8894}"
# Remove existing children (bucketed products only, not page components)
$productsRoot | Get-ChildItem | ? { $_.TemplateName -eq "Bucket" } | Remove-Item
# Generate IDs ahead of time so we can randomize related products too
$ids = 1..$num | % { (New-Guid).ToString("B").ToUpper() }
1..$num | % {
$id = $ids[$_-1]
$path = "/sitecore/content/Basic Company/Home/Products/$name"
$name = Get-Lorem -Minimum 2 -Maximum 4
$description = Get-Lorem -Minimum 5 -Maximum 8
# if we wanted to randomize the image, perhaps use unsplash source -- https://source.unsplash.com/
$image = "<image mediaid=""{D8B7E336-3EF6-4938-9E1A-8FE8BFEDB0FB}"" />"
$price = (Get-Random -Minimum 10 -Maximum 20) + 0.99
# get a random selection of a random number of generated id's, which aren't this product
$related = $ids | ? { $_ -ne $id } | Get-Random -Count (Get-Random -Minimum 4 -Maximum 9)
$features = Get-LoremUl -NumItems 3 -Minimum 2 -Maximum 4
$item = New-Item -ForceId $id -Path $path -ItemType $templateId
$item.Editing.BeginEdit()
$item["Short Description"] = $description
$item["Image"] = $image
$item["Features"] = $features
$item["Price"] = $price
$item["Related Products"] = $related -join "|"
$item.Editing.EndEdit() | out-null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment