From 0a89d8fbef243b1cbf0ba1ad4afcfcded8275844 Mon Sep 17 00:00:00 2001 From: Ganesh Sanap Date: Wed, 24 Jan 2024 23:29:05 +0530 Subject: [PATCH] Sample Update - Add dummy folders and files to a SharePoint library --- .../spo-add-dummy-folders-and-files/README.md | 162 ++++++++++++++---- .../assets/sample.json | 34 +++- 2 files changed, 152 insertions(+), 44 deletions(-) diff --git a/scripts/spo-add-dummy-folders-and-files/README.md b/scripts/spo-add-dummy-folders-and-files/README.md index b33d44fa0..11ee8c5fd 100644 --- a/scripts/spo-add-dummy-folders-and-files/README.md +++ b/scripts/spo-add-dummy-folders-and-files/README.md @@ -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)] diff --git a/scripts/spo-add-dummy-folders-and-files/assets/sample.json b/scripts/spo-add-dummy-folders-and-files/assets/sample.json index 970c41324..b45082066 100644 --- a/scripts/spo-add-dummy-folders-and-files/assets/sample.json +++ b/scripts/spo-add-dummy-folders-and-files/assets/sample.json @@ -2,14 +2,14 @@ { "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" ], @@ -17,16 +17,27 @@ { "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": [ { @@ -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", @@ -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" } ] }