Upload Random Documents to a SharePoint Environment

In my daily job I often need to kill a SharePoint farm and build a new one from scratch to match a client’s Production environment. When doing so, I also need to get rid of any test documents I had created in the previous environment, and need to start creating and uploading new documents from scratch for my new SharePoint environment. While working on a demo for one of my client, I need to demo a SharePoint migration, and also had to give them a live presentation of how the SharePoint 2013 Search Results will be displayed in the new environment.

Because of this requirement, I decided that I needed an automated way to upload thousands of documents, randomly, in my environment. Most of my dev SharePoint environments are single server farms (for simplicity and because it runs directly on Hyper-V on my work laptop). On these local farms, I always install the following software: Visual Studio, Office (2016 or 2013) and SharePoint Designer (yes I said it). The idea behind my automated script was that it should automatically generate Word documents, with random content in them, and upload them in a random site, somewhere in my environment, in the “Shared Documents” library that are included by default in Team Sites.

The first part of the script would have to rely on the Office Interop COM object, which means that ideally Office would have to be installed on the machine running the script. The first thing I did is create an array containing various nouns & verbs. Our script will randomly pick some of these words to populate the content of our Word documents.

$words = @("Cat", "Dog","Cow","Giraffe", "Ball", "Cube", "Sphere", "Document", "Paper", "Computer", "The", "Person", "Black", "Blue", "Red", "SharePoint", "Microsoft", "Red", "Search", "Create", "Delete", "Article", "Web", "HTML", "C#", "VB", "Microsoft", "Job", "Office", "Table", "Star", "Pen", "Pencil", "Arm", "Leg", "Doctor", "Student", "Education", "Defense", "Army", "Navy", "Boat", "Plane", "Jet", "Hockey", "Baseball", "Soccer", "Volley-Ball", "Swim", "Pool", "Lake", "Ocean", "Sea", "Bird", "Fly", "Sky", "Planet", "Space", "Pig", "Owl", "Night", "Day", "Morning", "Evening", "Delete", "Create", "Update", "Great", "Super", "Awesome", "Mark", "Check", "Report", "Minutes", "Decisions", "Technology","Software", "Hardware", "Server", "Database", "Application", "Game", "Keyboard", "Mouse", "Screen", "Tower", "Building", "Street", "Song", "Music", "Car", "Bus")

Next I will be prompting the users to ask them how many documents they wish to randomly upload across the environment. The script will then loop as many times as specified by the user, will generate a random document on each iteration, select a random SPWeb somewhere in the environment and upload the document in its Shared Documents library. Each document will be given a GUID as filename to make sure we don’t hit any conflicts.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$words = @("Cat", "Dog","Cow","Giraffe", "Ball", "Cube", "Sphere", "Document", "Paper", "Computer", "The", "Person", "Black", "Blue", "Red", "SharePoint", "Microsoft", "Red", "Search", "Create", "Delete", "Article", "Web", "HTML", "C#", "VB", "Microsoft", "Job", "Office", "Table", "Star", "Pen", "Pencil", "Arm", "Leg", "Doctor", "Student", "Education", "Defense", "Army", "Navy", "Boat", "Plane", "Jet", "Hockey", "Baseball", "Soccer", "Volley-Ball", "Swim", "Pool", "Lake", "Ocean", "Sea", "Bird", "Fly", "Sky", "Planet", "Space", "Pig", "Owl", "Night", "Day", "Morning", "Evening", "Delete", "Create", "Update", "Great", "Super", "Awesome", "Mark", "Check", "Report", "Minutes", "Decisions", "Technology","Software", "Hardware", "Server", "Database", "Application", "Game", "Keyboard", "Mouse", "Screen", "Tower", "Building", "Street", "Song", "Music", "Car", "Bus")

$number = Read-Host "How many documents do you want to randomly upload to SharePoint?"

$allWebApps = Get-SPWebApplication

[ref]$SaveFormat = "Microsoft.Office.Interop.Word.WdSaveFormat" -as [type]
$word = New-Object -ComObject word.application
$word.visible = $false
for($i = 0; $i -lt $number; $i++)
{
    $filePath = "C:\DND\Documents\" + [guid]::NewGuid().ToString() + ".docx"
    
    $doc = $word.documents.add()

    $selection = $word.selection
    $result = $selection.WholeStory
    $selection.Style = "No Spacing"

    $rndNumberOfWords = Get-Random -Minimum 1 -Maximum 1000
    $sentence = ""
    for($j = 0; $j -le $rndNumberOfWords; $j++)
    {
        $sentence += $words[(Get-Random -Minimum 0 -Maximum ($words.Length))] + " "
    }

    $selection.font.size = 14
    $selection.font.bold = 1
    $selection.typeText($sentence)

    $result=$doc.saveas([ref] $filePath, [ref]$saveFormat::wdFormatDocument)
    $result = $doc.Close()

    $randomWebApp = $allWebApps[(Get-Random -Minimum 0 -Maximum $allWebApps.Length)]
    $randomSite = $randomWebApp.Sites[(Get-Random -Minimum 0 -Maximum ($randomWebApp.Sites.Count-1))]
    
    $url = $randomSite.Url
    Write-Host "Uploading a random document to"$url

    $spWeb = Get-SPWeb $url 
    $docLib = $spWeb.GetFolder("Shared Documents") 
    $allFiles = $docLib.Files 

    $fileName = $filePath.Substring($filePath.LastIndexOf("\")+1) 

    $fileObject = Get-ChildItem $filePath

    $stream = $fileObject.OpenRead()
    $result = $allFiles.Add("Shared Documents/" + $fileName, $stream, $false)
    $stream.Close()
    $fileObject = $null

    $spWeb.Dispose()
    Remove-Item $filePath -ErrorAction SilentlyContinue -Force
}
$word = $null

Microsoft Premier Field Engineer – SharePoint

One thought on “Upload Random Documents to a SharePoint Environment

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*