SharePoint PowerShell How To: Create SharePoint List Items (in Root and in Folders) for Load/Performance Testing

SharePoint PowerShell How To Series – Foreword

I often find myself scratching around the local drive of my development machines for remnants of PowerShell scripts to generate list & libraries in SharePoint with all sorts of different folder hierarchies to test the performance of code I’m working on to ensure it scales well once the folder & item counts start getting up to high numbers. So rather than keep scratching I’m going to start posting my scripts as I create and use them so I can find/reuse them later on and someone else might find them useful as well. I’m sure they won’t work under all scenarios and situations (and they are not designed to). They are just quick scripts that serve a specific purpose to me at the time and you may be able to tweak to fit your needs.

Create list items in the root of a custom SharePoint list

Script description: Creates 10,000 list items in the root folder of the “OnePlace License R7” list of the site located at the URL “vs-server38”.

#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

# Script settings
$webUrl = "http://vs-server38"
$listName = "OnePlace Licenses R7"
$numberItemsToCreate = 10000
$itemNamePrefix = "License "

# Open web and library
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

# Create desired number of items in subfolder
for($i=1; $i -le $numberItemsToCreate; $i++)
{
$newItemSuffix = $i.ToString("00000")
$newItem = $list.AddItem()
$newItem["Title"] = "$itemNamePrefix$newItemSuffix"
$newItem.Update()
write-host "Item created: $itemNamePrefix$newItemSuffix"
}

#Dispose web
$web.Dispose()

Create list items in a folder of a custom SharePoint list

Script description: Creates 10,000 list items in the “OnePlaceDocs” folder of the “OnePlace License R7” list of the site located at the URL “vs-server38”.

Note: this assumes you have already created a folder called “OnePlaceDocs”in the list.

#Add SharePoint PowerShell Snapin which adds SharePoint specific cmdlets
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

# Script settings
$webUrl = "http://vs-server38"
$listName = "OnePlace Licenses R7"
$subFolderName = "OnePlaceDocs"
$numberItemsToCreate = 10000
$itemNamePrefix = "License "

# Open web and library
$web = Get-SPWeb $webUrl
$list = $web.Lists[$listName]

# Get handle on the subfolder
$subFolder = $list.RootFolder.SubFolders.Item($subFolderName);

# Create desired number of items in subfolder
for($i=1; $i -le $numberItemsToCreate; $i++)
{
$newItemSuffix = $i.ToString("00000")
$newItem = $list.AddItem($subFolder.ServerRelativeUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
$newItem["Title"] = "$itemNamePrefix$newItemSuffix"
$newItem.Update()
write-host "Item created: $itemNamePrefix$newItemSuffix"
}

#Dispose web
$web.Dispose()

Others articles in this series:

4 thoughts on “SharePoint PowerShell How To: Create SharePoint List Items (in Root and in Folders) for Load/Performance Testing

Add yours

  1. Hi,
    How would you add an item in a subfolder of the subfolder. I’m getting an out of range error if I try that.

    Greetings,
    George

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑