Skip to content

Commit

Permalink
Merge pull request #646 from ganesh-sanap/spo-add-dummy-folders-and-f…
Browse files Browse the repository at this point in the history
…iles-cli

Sample Update - Add dummy folders and files to a SharePoint library
  • Loading branch information
pkbullock authored Jan 26, 2024
2 parents fb7a9b9 + 0a89d8f commit 574f307
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 44 deletions.
162 changes: 124 additions & 38 deletions scripts/spo-add-dummy-folders-and-files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,80 +6,166 @@ plugin: add-to-gallery

## Summary

This sample shows how to add dummy files and folders into a library. The script was used to generate files within folders to perform some testing.

This sample shows how to add dummy folders and files into a SharePoint document library. The script was used to generate files within folders to perform some testing.

## Implementation

Open Windows Powershell ISE

Create a new file, e.g. TestDoc.docx and save it to a file server location , e.g. C:/Temp

A loop within another loop using while is used to create the number of specified files within each of the specified number of folders.
1. Open Windows PowerShell ISE.
2. Create a new file, e.g. TestDoc.docx and save it to a file server location, e.g. C:/Temp.
3. A loop within another loop using `While` is used to create the number of specified files within each of the specified number of folders.

# [PnP PowerShell](#tab/pnpps)

```powershell
#Parameters
# Parameters
$SiteURL = "https://contoso.sharepoint.com/sites/Company311"
#Library in which to create the dummy files and folders
# Library in which to create the dummy files and folders
$LibraryName = "LargeLibrary"
#Location of the dummy file
# Location of the dummy file
$LocalFile= "C:\Temp\TestDoc.docx"
#Number of files to create within each folder
# Number of files to create within each folder
$MaxFilesCount = 20
#Number of folders to create in the libraru
# Number of folders to create in the libraru
$MaxFolderCount = 500
#The name of the folder to be created
# The name of the folder to be created
$FolderName = "Folder"
Try {
#Get the File from file server
# Get the File from file server
$File = Get-ChildItem $LocalFile
# Connect to SharePoint online site
Connect-PnPOnline -Url $SiteURL -Interactive
# Initialize folder counter
$FolderCounter = 1
While($FolderCounter -le $MaxFolderCount)
{
$newFolderName = $FolderName +"_"+ $FolderCounter
try{
Add-PnPFolder -Name $newFolderName -Folder "$($LibraryName)" | Out-Null
Write-host -f Green "New Folder '$newFolderName' Created ($FolderCounter of $MaxFolderCount)!"
$FileCounter = 1
While($FileCounter -le $MaxFilesCount)
{
$NewFileName= $File.BaseName+"_"+$FileCounter+".docx"
Try{
Add-PnPFile -Path $File -Folder "$($LibraryName)/$newFolderName" -NewFileName $NewFileName | Out-Null
}
Catch{
Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
$newFolderName = $FolderName + "_" + $FolderCounter
Try {
# Add new folder in the library
Add-PnPFolder -Name $newFolderName -Folder "$($LibraryName)" | Out-Null
Write-Host -f Green "New Folder '$newFolderName' Created ($FolderCounter of $MaxFolderCount)!"
# Initialize file counter
$FileCounter = 1
While($FileCounter -le $MaxFilesCount)
{
$NewFileName = $File.BaseName + "_" + $FileCounter + ".docx"
Try {
# Add new file in the folder
Add-PnPFile -Path $File -Folder "$($LibraryName)/$newFolderName" -NewFileName $NewFileName | Out-Null
}
Catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host -f Green "New File '$NewFileName' Created ($FileCounter of $MaxFilesCount)!"
$FileCounter++
}
Write-host -f Green "New File '$NewFileName' Created ($FileCounter of $MaxFilesCount)!"
$FileCounter++
}
}
Catch{
Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}
$FolderCounter++;
Catch {
Write-Host "Error: $($_.Exception.Message)" -ForegroundColor Red
}
$FolderCounter++;
}
}
Catch {
Write-Host -f Red "Error Uploading File:"$_.Exception.Message
}
```

[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]

# [CLI for Microsoft 365](#tab/cli-m365-ps)

```powershell
# SharePoint online site URL
$SiteURL = Read-Host -Prompt "Enter your SharePoint site URL (e.g https://contoso.sharepoint.com/sites/Company311)"
# Document library URL where you want to create the dummy folders and files
$LibraryName = Read-Host -Prompt "Enter site-relative URL of your Document library (e.g '/Shared Documents')"
# Location of the dummy file
$LocalFile= "D:\dtemp\TestDoc.docx"
# Number of files to create within each folder
$MaxFilesCount = 20
# Number of folders to create in the libraru
$MaxFolderCount = 500
# The name of the folder to be created
$FolderName = "Folder"
# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
Try {
# Get the File from file server
$File = Get-ChildItem $LocalFile
# Initialize folder counter
$FolderCounter = 1
While($FolderCounter -le $MaxFolderCount)
{
$newFolderName = $FolderName + "_" + $FolderCounter
Try {
# Add new folder in the library
m365 spo folder add --webUrl $SiteURL --parentFolderUrl $LibraryName --name $newFolderName
Write-Host -f Green "New Folder '$newFolderName' Created ($FolderCounter of $MaxFolderCount)!"
# Initialize file counter
$FileCounter = 1
While($FileCounter -le $MaxFilesCount)
{
$NewFileName = $File.BaseName + "_" + $FileCounter + ".docx"
Try {
# Add new file in the folder
m365 spo file add --webUrl $SiteURL --folder "$($LibraryName)/$newFolderName" --path $File --FileLeafRef $NewFileName
}
Catch {
Write-Host "Error while creating a new file: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host -f Green "New File '$NewFileName' Created ($FileCounter of $MaxFilesCount)!"
$FileCounter++
}
}
Catch {
Write-Host "Error while creating a new folder: $($_.Exception.Message)" -ForegroundColor Red
}
$FolderCounter++;
}
}
Catch {
write-host -f Red "Error Uploading File:"$_.Exception.Message
write-host -f Red "Error Uploading File:"$_.Exception.Message
}
# Disconnect SharePoint online connection
m365 logout
```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]

[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)]

***

## Contributors

| Author(s) |
|-----------|
| [Reshmee Auckloo](https://github.com/reshmee011)|
| [Ganesh Sanap](https://ganeshsanapblogs.wordpress.com/about) |

[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]

Expand Down
34 changes: 28 additions & 6 deletions scripts/spo-add-dummy-folders-and-files/assets/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,42 @@
{
"name": "spo-add-dummy-folders-and-files",
"source": "pnp",
"title": "Add dummy folders and files.",
"shortDescription": "This sample shows how to add dummy files and folders into a library, generating files within folders to perform testing.",
"title": "Add dummy folders and files to a SharePoint library",
"shortDescription": "This sample shows how to add dummy folders and files into a SharePoint library, generating files within folders to perform testing.",
"url": "https://pnp.github.io/script-samples/spo-add-dummy-folders-and-files/README.html",
"longDescription": [
"This sample shows how to add dummy files and folders into a library. The script was used to generate files within folders to perform some testing."
"This sample shows how to add dummy folders and files into a SharePoint document library. The script was used to generate files within folders to perform some testing."
],
"creationDateTime": "2022-11-13",
"updateDateTime": "2022-11-13",
"updateDateTime": "2024-01-24",
"products": [
"SharePoint"
],
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "1.12.0"
},
{
"key":"CLI-FOR-MICROSOFT365",
"value":"7.3.0"
}
],
"categories": [
"Configure",
"Security"
"Data",
"Provision"
],
"tags": [
"Connect-PnPOnline",
"Add-PnPFolder",
"Add-PnPFile"
"Add-PnPFile",
"m365 status",
"m365 login",
"m365 spo folder add",
"m365 spo file add",
"m365 logout",
"Get-ChildItem"
],
"thumbnails": [
{
Expand All @@ -37,6 +48,12 @@
}
],
"authors": [
{
"gitHubAccount": "ganesh-sanap",
"company": "",
"pictureUrl": "https://avatars.githubusercontent.com/u/25476310?v=4",
"name": "Ganesh Sanap"
},
{
"name": "Reshmee Auckloo",
"gitHubAccount": "reshmee011",
Expand All @@ -49,6 +66,11 @@
"name": "Want to learn more about PnP PowerShell and the cmdlets",
"description": "Check out the PnP PowerShell site to get started and for the reference to the cmdlets.",
"url": "https://aka.ms/pnp/powershell"
},
{
"name": "Want to learn more about CLI for Microsoft 365 and the commands",
"description": "Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.",
"url": "https://aka.ms/cli-m365"
}
]
}
Expand Down

0 comments on commit 574f307

Please sign in to comment.