Friday, February 9, 2018

Powershell to add dummy documents to SharePoint List for Testing purpose

Write-Host "Loading SharePoint Powershell Snapin"
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
if ($snapin -eq $null) {  Add-PSSnapin "Microsoft.SharePoint.Powershell" }

# ---- Script settings ----
$sourceDocumentPath = "C:\temp\TestDoc.docx" # Source document to spawn new documents from for the creation
$newFilenamePrefix = "TestDoc"
$newFilenameExtension = ".docx"
$numberDocsToCreate = 5000

# Settings for the destination to create documents in

$webUrl = "http://site:1111/sites/tdm3"
$docLibraryName = "Lib"
$folderPathWithinDocLibrary = "" # Leave empty e.g. "" to create documents in root folder of library otherwise specify path relative to root folder e.g. "/Testing/Folder A"

# -------------------------

#Open web and library
$web = Get-SPWeb $webUrl
$docLibrary = $web.Lists[$docLibraryName]
$docLibraryUrl = $docLibrary.RootFolder.ServerRelativeUrl
$uploadfolder = $web.getfolder($docLibraryUrl + $folderPathWithinDocLibrary)

#Open file
$file = get-item $sourceDocumentPath
$fileStream = ([System.IO.FileInfo] (Get-Item $file.FullName)).OpenRead()

# Create documents in SharePoint
write-host "Creating $i documents based on the file $sourceDocumentPath"

for($i=1; $i -le $numberDocsToCreate; $i++)
{
$newFilePath = $docLibraryUrl + $folderPathWithinDocLibrary + "/" + $newFilenamePrefix+$i+$newFilenameExtension
write-host "Creating document: $newFilePath ..."
$spFile = $uploadfolder.Files.Add($newFilePath, [System.IO.Stream]$fileStream, $true)
}

write-host "Completed"

#Close file stream
$fileStream.Close()

#Dispose web
$web.Dispose()

No comments:

Post a Comment