SharePoint Site Population Script for Testing
Summary
The following script will create and populate data in a SharePoint Farm for testing.
What does the Script do?
- Create a new web application, with a new web application pool.
- Create new database and associate it with the new web application.
- Update the local HOSTS file to make the new site accessible from the server.
- Create a Root Site in the new web application.
- Create a Test document and store it in the local temp folder.
- Populate the default "Document Library" in the root site with the number of documents specified.
- Create new lists in the root site based on the number of lists specified.
- Populate each list in the root site with the number of items specified.
- Create new sub sites based on the amount specified.
- Populate the default "Document Library" in all sub sites with the number of documents specified.
- Create new lists in each sub site based on the number of lists specified.
- Populate each list in all sub sites with the number of items specified.
Important Notes:
- Since this script will update your local HOSTS file you will need to run PowerShell as Administrator.
- DisableLoopBackCheck may be required if you access the site locally from the server.
reg.exe add HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa /v DisableLoopBackCheck /t REG_DWORD /d 1 /f
Example:
How many Sites should be created?: 1
How many Lists should be created?: 2
How many Items in each list?: 5
How many Documents should be created?: 5
Specify the full URL of the new SharePoint Site, without https://. Example: contoso, contoso.local or www.contoso.com: sptest2
Enter your SQL Server Name: michlee-sql
Name of the new Content DB: sptest2_content_db
Enter the SP Managed Account. Example: contoso\spadmin: mylab\spservice
Enter a name for the new Web App: SPTest2 Web App
Enter a name for the new AppPool: sptest2apppool
The new Web Application sptest2 is being created. Please stand by... The new Content Database sptest2_content_db is being created. Please stand by... Id : eb6f2c05-e7d8-491a-9b6f-0a17b2624c65
Name : sptest2_content_db
WebApplication : SPWebApplication Name=SPTest2 Web App
Server : michlee-sql
CurrentSiteCount : 0
127.0.0.1 sptest2
The new Root Site https://sptest2 is being created. Please stand by... Creating document: /Shared Documents/TestDocument1.csv ... Creating document: /Shared Documents/TestDocument2.csv ... Creating document: /Shared Documents/TestDocument3.csv ... Creating document: /Shared Documents/TestDocument4.csv ... Creating document: /Shared Documents/TestDocument5.csv ... Guid : 5dd9162c-08bd-4fd1-8079-6c3e106386c8
################################## List 1 created in https://sptest2/ ################################# Item: 1 created in list: 1 ################################ Item: 2 created in list: 1 ################################ Item: 3 created in list: 1 ################################ Item: 4 created in list: 1 ################################ Item: 5 created in list: 1 ################################ Guid : c81edcd1-71d4-427a-8cd6-a0015a712149
################################## List 2 created in https://sptest2/ ################################## Item: 1 created in list: 2 ################################ Item: 2 created in list: 2 ################################ Item: 3 created in list: 2 ################################ Item: 4 created in list: 2 ################################ Item: 5 created in list: 2 ################################ #################### Creating Sub Site 1 #################### Creating document: /1/Shared Documents/TestDocument1.csv ... Creating document: /1/Shared Documents/TestDocument2.csv ... Creating document: /1/Shared Documents/TestDocument3.csv ... Creating document: /1/Shared Documents/TestDocument4.csv ... Creating document: /1/Shared Documents/TestDocument5.csv ... Guid : 585c9288-d72a-472d-8532-5a3139537777
################################## List 1 created in https://sptest2/1 ################################## Item: 1 created in list: 1 ################################ Item: 2 created in list: 1 ################################ Item: 3 created in list: 1 ################################ Item: 4 created in list: 1 ################################ Item: 5 created in list: 1 ################################ Guid : a49c9124-de8f-4174-bd12-3958ec1fd62b
################################### List 2 created in https://sptest2/1 ################################### Item: 1 created in list: 2 ################################ Item: 2 created in list: 2 ################################ Item: 3 created in list: 2 ################################ Item: 4 created in list: 2 ################################ Item: 5 created in list: 2 ################################ ########################################## Your Site population script has completed! ##########################################
The Script
# Check to ensure Microsoft.SharePoint.PowerShell is loaded
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin Microsoft.SharePoint.Powershell
}
#Script Parameters
$sitecount = (Read-Host "How many Sites should be created?")
$listcount = (Read-Host "How many Lists should be created?")
$itemcount = (Read-Host "How many Items in each list?")
$doccount = (Read-Host "How many Documents should be created?")
$url = (Read-Host "Specify the full URL of the new SharePoint Site, without https://. Example: contoso, contoso.local or www.contoso.com")
$sqlsvr = (Read-Host "Enter your SQL Server Name")
$sqldb = (Read-Host "Name of the new Content DB")
$sitowner = (Read-Host "Enter the SP Managed Account. Example: contoso\spadmin")
$sitename = (Read-Host "Enter a name for the new Web App")
$apppool = (Read-Host "Enter a name for the new AppPool")
$is2010 = (Read-Host "Is this a 2010 Farm Y/N?")
#Adding 2010 capability
if ($is2010 -eq "Y"){
$docLibraryName = "Shared Documents"
}
if ($is2010 -eq "N"){
$docLibraryName = "Documents"
}
#Static Settings
$sourceDocumentPath = "$env:TEMP\temp\TestDocument.csv"
$newFilenamePrefix = "TestDocument"
$newFilenameExtension = ".csv"
$hostsfile = "$env:windir\System32\drivers\etc\hosts"
$spaccount = (Get-SPManagedAccount $sitonwer)
$user2 = whoami
#Create the new web app
Write-Host "The new Web Application $url is being created. Please stand by..." -ForegroundColor Green
$site = "https://" + $url
$ap = New-SPAuthenticationProvider
$webapp = New-SPWebApplication -Name $sitename -URL $site -HostHeader $url -Port 80 -ApplicationPool $apppool -ApplicationPoolAccount $spaccount -AuthenticationProvider $ap
#Create a new Content Database
Write-Host "The new Content Database $sqldb is being created. Please stand by..." -ForegroundColor Yellow
New-SPContentDatabase $sqldb -DatabaseServer $sqlsvr -WebApplication $webapp
#This will update your HOSTS file with the named used in the URL so the site is accessible from the server
#Note: This will require that PowerShell runs in elevated mode
"127.0.0.1 $url" | Add-Content -PassThru $hostsfile
#Create Root Site
Write-Host "The new Root Site $site is being created. Please stand by..." -ForegroundColor Yellow
$SiteTemplate = "STS#0"
$Language = 1033
New-SPSite -Url $site -OwnerAlias $sitowner -SecondaryOwnerAlias $user2 -Template $SiteTemplate -Language $Language
#Test file creation
#make a new folder if it does not exist
$TARGETDIR = "$env:TEMP\temp"
if(!(Test-Path -Path $env:TEMP\temp)){
New-Item -ItemType directory -Path $TARGETDIR
}
dir | export-csv "$env:TEMP\temp\TestDocument.csv"
##################################
#Root WEB Creation and Population#
##################################
#defining WEB
$web = Get-SPWeb $site
#Create Documents
$docLibrary = $web.Lists[$docLibraryName]
$docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl
$folderPathWithinDocLibrary = ""
$uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary)
#Open file
$file = get-item $sourceDocumentPath
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
#Starting loop create docs in Root Library
for($d=1; $d -le $doccount; $d++)
{
$newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$d+$newFilenameExtension
write-host "Creating document: $newFilePath ..." -ForegroundColor Yellow
$spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true)
}
#Start Loop for List creation based on $listcount in ROOT Library
for($l=1; $l -le $listcount; $l++)
#Create the Lists
{
$ListTemplate = $web.ListTemplates["Custom List"]
$web.Lists.Add("List $l","List $l",$listTemplate)
Write-Host "####################################" -ForegroundColor Green
write-host "List $l created in $site" -ForegroundColor Green
Write-Host "####################################" -ForegroundColor Green
# While creating lists put new items in it based on the $itemcount
if ($l -le $listcount )
{
#Start Loop for List Item creation in ROOT Library
for ($i=1; $i -le $itemcount; $i++)
{
#Create List Item
$list = $web.Lists["List $l"]
$newItem = $list.AddItem()
$newItem["Title"] = "Item $i"
$newItem.Update()
write-host "Item: $i created in list: $l" -ForegroundColor Yellow
Write-Host "##############################" -ForegroundColor Yellow
}}}
#################################
#Sub WEB Creation and Population#
#################################
#Start Loop for site creation
for($s=1; $s -le $sitecount ; $s++)
{
$SiteUrl = ""
$SiteUrl = $Site + "/"
$SiteUrl = $SiteUrl += $s
#Create Webs based on $sitecount
Write-Host "####################" -ForegroundColor Yellow
Write-Host "Creating Sub Site $s" -ForegroundColor Yellow
Write-Host "####################" -ForegroundColor Yellow
New-SPWeb $SiteUrl -Template $SiteTemplate -Name $s -UseParentTopNav -Language $Language
$web = Get-SPWeb $SiteUrl
#Create Documents
$docLibrary = $web.Lists[$docLibraryName]
$docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl
$folderPathWithinDocLibrary = ""
$uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary)
#Open file
$file = get-item $sourceDocumentPath
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()
#Starting loop to create docs in Library
for($d=1; $d -le $doccount; $d++)
{
$newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$d+$newFilenameExtension
write-host "Creating document: $newFilePath ..." -ForegroundColor Yellow
$spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true)
}
#Start Loop for List creation based on $listcount
for($l=1; $l -le $listcount; $l++)
#Create the Lists
{
$ListTemplate = $web.ListTemplates["Custom List"]
$web.Lists.Add("List $l","List $l",$listTemplate)
Write-Host "########################################" -ForegroundColor Green
write-host "List $l created in $SiteUrl" -ForegroundColor Green
Write-Host "########################################" -ForegroundColor Green
#Populate list items based on the $itemcount
if ($l -le $listcount )
{
#Start Loop for List Item creation
for ($i=1; $i -le $itemcount; $i++)
{
#Create List Item
$list = $web.Lists["List $l"]
$newItem = $list.AddItem()
$newItem["Title"] = "Item $i"
$newItem.Update()
write-host "Item: $i created in list: $l" -ForegroundColor Yellow
Write-Host "################################" -ForegroundColor Yellow
}}}}
#Close file stream
$fileStream.Close()
#Dispose web
$web.Dispose()
Write-Host "##########################################" -ForegroundColor Green
write-host "Your Site population script has completed!" -ForegroundColor Green
Write-Host "##########################################" -ForegroundColor Green
Comments
- Anonymous
February 06, 2018
Great article. This is the dream script for people planning to do capacity tests.- Anonymous
February 06, 2018
Thanks Rodney!
- Anonymous
- Anonymous
February 15, 2018
Great script Mike! I borrowed part of this to populate a SPWeb I have to create 1000 lists, with 2000 items per list ( so 2 million items ). I need it to test some search functionality.. I did a little tweaking to add Fields to the List and also add those fields to the default View. I also broke it out a bit to populate each item with unique values across fields.. this saved me quite a bit of time!- Anonymous
February 16, 2018
Great...glad it helped! Share your modifications :)- Anonymous
February 16, 2018
The comment has been removed- Anonymous
February 16, 2018
Perfect Thanks!
- Anonymous
- Anonymous
- Anonymous
- Anonymous
February 21, 2018
Great Article Mike..Script is very helpful for load testing and capacity planning......... - Anonymous
February 06, 2019
Greate :)