Skip to content

Instantly share code, notes, and snippets.

@Kieranties
Last active December 22, 2016 14:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kieranties/9545049 to your computer and use it in GitHub Desktop.
Save Kieranties/9545049 to your computer and use it in GitHub Desktop.
Simple script to execute commands against the FillDB page in Sitecore 7.2
# Example of running Invoke-FillDB.ps1
# Additional files to download
$downloads= @(
"http://www.gutenberg.org/files/42698/42698-0.txt",
"http://www.gutenberg.org/cache/epub/42705/pg42705.txt",
"http://www.gutenberg.org/cache/epub/42710/pg42710.txt",
"http://www.gutenberg.org/files/42700/42700-0.txt",
"http://www.gutenberg.org/files/42706/42706-0.txt"
)
# Custom template
$template = "{1C7E374B-44FC-4E33-8F8B-857D1A32CDC3}"
# Additional fields on a custom template to populate
$fields= @(
"{DB074AA7-DF72-46C5-8269-E4A06FC8EC74}",
"{6AB6FC48-D23C-465D-8D11-6BECB76C64CC}",
"{D5E42CBE-3D6A-470F-A7B6-AC4EC7CF72D3}",
"{3293A6C1-3E6F-45F3-9170-6D2D133C4304}",
"{A4562857-67CC-48D3-9424-A0BF37C7F429}",
"{EA0C14FF-2868-4F9A-8FCF-236B0DBCF86B}"
)
$site = "http://sitecore.testsite"
$username = "sitecore\username"
$password = "password"
# Invoke-FillDB performing all basic steps
$response = .\Invoke-FillDB.ps1 $site $username $password -clearCache -runSqlScript `
-createWordsDir -wordsDir "words" -downloadFiles $downloads -templateFields $fields `
-templateGuid $template -prefix "Standard" `
-wordsPerField 100 -itemCount 100 -indexName "sitecore_master_index"
# See the response
$response.messages
$response.success
# Invoke-FillDB only creating items
$response = .\Invoke-FillDB.ps1 $site $username $password -itemCount 100 -clearcache
# See the response
$response.messages
$response.success
<#
.SYNOPSIS
Run FillDB.aspx on a Sitecore instance
.DESCRIPTION
Processes the FillDB.aspx admin page to create new items in Sitecore
#>
param(
# The full url of the site to run FillDB on
$site,
# The user to login as - must have access to FillDb page
$username,
# The password of the user
$password,
# The database to run content into. Defaults to "master"
$database = "master",
# The directory to create/store/read txt files. Defaults to "/data/words"
$wordsDir = "/data/words",
# Set if the preparation sql script should be ran.
[switch]$runSqlScript,
# The sql preparation script. Defaults to "/sitecore/admin/SqlScripts/ItemGenerator.sql"
$sqlScript = "/sitecore/admin/SqlScripts/ItemGenerator.sql",
# Set if the words directory should be initialized
[switch]$createWordsDir,
# Set if the cache should be cleared
[switch]$clearCache,
# A collection of files to download
[string[]]$downloadFiles,
# The number of items to generate
[int]$itemCount = 0,
# The guid to create items under. Defaults to "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}" (Home)
$parentGuid = "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}",
# The template guid to create items based on. Defaults to "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}" (Sample Item)
$templateGuid = "{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}",
# The fields of the template to populate. Defaults to "{A60ACD61-A6DB-4182-8329-C957982CEC74}","{75577384-3C97-45DA-A847-81B00500E250})" (Title and Text of Sample Item)
[string[]]$templateFields = @("{A60ACD61-A6DB-4182-8329-C957982CEC74}","{75577384-3C97-45DA-A847-81B00500E250}"),
# The prefix to add to the name of created items. Defaults to "Auto"
$prefix = "Auto",
# The maxium number of words to set in a field. Defaults to 25
[int]$wordsPerField = 25,
# Set if the indexes should be rebuilt.
[switch]$runIndex,
# The index to rebuild after items are inserted.
[string]$indexName,
# The item ids to index from.
[String[]]$indexRoots
)
Function Get-SitecoreSession{
# Login - to create web session with authorisation cookies
$loginPage = "$site/sitecore/admin/login.aspx"
$login = Invoke-WebRequest $loginPage -SessionVariable webSession
$form = $login.forms[0]
$form.fields["LoginTextBox"] = $username
$form.fields["PasswordTextBox"] = $password
Invoke-WebRequest -Uri $loginPage -WebSession $webSession -Method POST -Body $form | Out-Null
$webSession
}
# Parameters to post to fill db page
$body = @{
database = $database
wordsDir = $wordsDir
runsql = $runSqlScript
sqlScript = $sqlScript
runWordsDir = $createWordsDir
runDownloadFiles = $downloadFiles.Count -gt 0
downloadFiles = $downloadFiles -join ","
runCacheClear = $clearCache
runGenerate = $itemCount -gt 0
itemCount = $itemCount
parentGuid = $parentGuid
templateGuid = $templateGuid
templateFields = $templateFields -join ","
namePrefix = $prefix
wordsPerField = $wordsPerField
runIndex = $runIndex -or (-not [string]::IsNullOrEmpty($indexName)) -or ($indexRoots.Length -gt 0)
indexName = $indexName
indexRoots = $indexRoots -join ","
json = $true
}
$session = Get-SitecoreSession
$fillPage = "$site/sitecore/admin/filldb.aspx"
Invoke-RestMethod -Uri $fillPage -WebSession $session -Method POST -Body $body
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment