Scenario
Sometimes you want to create some random sample data to be used in a SharePoint Online list. For example, in yesterday’s post I was using a list with some data which I entered manually, but I thought afterwards that I could’ve scripted it to create many more items in my demo list. Today, I show you how you can easily fill a list in SharePoint Online with as many sample items as you need, all with random values based on some predefined arrays.
Code
The code below requires a few things from you:
- You need to define the number of items you want to create
- You need to define a few arrays with potential values for each of your columns (in my case, $Company, $Region, and $Product)
- You need to define the list where you want to add those items to
- Lastly, you need to update the -Values parameter with the correct column names for your list
$itemsToGenerate = 10 $listName = "Sales Pipeline" # Sample data for the list $Company = ("Ah Loong Pte Ltd", "Contoso Inc", "Fabrikam Corp", "Flowers by Irene",` "NorthSouth Trading Inc", "Petrox Oil Company", "Spacely Sprockets", "The Frying Dutchman") $Region = ("North", "East", "South", "West") $Product = ("Admin ToolKit", "EZClean", "WonderTool 2000", "Business Process WonderKit") $cred = Get-Credential Connect-PnPOnline -Url https://mytenant.sharepoint.com/sites/demo -Credentials $cred foreach($i in 1..$itemsToGenerate) { #Update the -Values below with the correct column names from your list Add-PnPListItem -List $listName -Values @{` # I'm assigning a randomly chosen value from the $Company array to the item column "Title" = $Company[(Get-Random -Minimum 0 -Maximum ($Company.Count))];` "Region" = $Region[(Get-Random -Minimum 0 -Maximum ($Region.Count))];` "Product" = $Product[(Get-Random -Minimum 0 -Maximum ($Product.Count))];` # Here, I want to create a random number between 150 and 99999 "Potential_x0020_Value" = (Get-Random -Minimum 150 -Maximum 99999);} }
Office 365 Advent Calendar – 11 Adding sample items with random data to a SharePoint Online List https://t.co/T2G7Wj6o3W #Office365
Office 365 Advent Calendar – 11 Adding sample items with random data to a SharePoint Online List https://t.co/WpDbKPrRF8 #office365
Office 365 Advent Calendar – 11 Adding sample items with random data to a SharePoint Online List https://t.co/ibnDTr17tj #Office365