Introduction

SharePointPnP.PowerShell solution contains a library of PowerShell commands, which allows you to perform complex provisioning and artifact management actions towards SharePoint. The commands use CSOM and can work against both SharePoint Online as SharePoint On-Premises.

Prerequisites

Install SharePointPnPPowerShellOnline.msi for SharePoint Online.

Upload files

Open Notepad and paste XML given below. Save the text file as Inputs.xml.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <Inputs>
  3. <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="xxxxxxxxx"></ConnectSPOnline>
  4. <UploadFiles>
  5. <UploadFile SourceFolder=".\HTML" DestinationFolder="SiteAssets" CreateFolder="yes" FolderName="HTML"></UploadFile>
  6. <UploadFile SourceFolder=".\CSS" DestinationFolder="SiteAssets" CreateFolder="yes" FolderName="CSS"></UploadFile>
  7. <UploadFile SourceFolder=".\Files" DestinationFolder="SiteAssets" CreateFolder="no" FolderName=""></UploadFile>
  8. </UploadFiles>
  9. </Inputs>

Open Notepad and paste the script given below. Save the text file as uploadfiles.ps1.

  1. ############################################################## Logging #########################################
  2. $date= Get-Date -format MMddyyyyHHmmss
  3. start-transcript -path .\Log_$date.doc
  4. ################################################### Get input parameters from XML ###############################
  5. # Get content from XML file
  6. [xml]$xmlData=Get-Content ".\Inputs.xml"
  7. # ConnectSPOnline node
  8. [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline
  9. $siteURL=$connectSPOnline.SiteURL
  10. $userName=$connectSPOnline.UserName
  11. $password=$connectSPOnline.Password
  12. # UploadFiles node
  13. [System.Xml.XmlElement]$uploadFiles = $xmlData.Inputs.UploadFiles
  14. ########################################################## Get Credentials ######################################
  15. function GetCredentials()
  16. {
  17. write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL
  18. # Convert password to secure string
  19. $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force
  20. # Get the credentials
  21. $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd
  22. # Connect to SP online site
  23. Connect-PnPOnline –Url $siteURL –Credentials $credentials
  24. }
  25. ########################################################### Upload Files ########################################
  26. function UploadFiles()
  27. {
  28. write-host -ForegroundColor Green "Upload files to SP Online site"
  29. # Loop through UploadFiles XML node
  30. foreach($uploadFile in $uploadFiles.UploadFile)
  31. {
  32. # UploadFile node parameters
  33. $ufSourceFolder=$uploadFile.SourceFolder
  34. $upDestinationFolder=$uploadFile.DestinationFolder
  35. $ufCreateFolder=$uploadFile.CreateFolder
  36. $ufFolderName=$uploadFile.FolderName
  37. # Check whether folder has to be created
  38. if($ufCreateFolder="yes")
  39. {
  40. $siteRelativePath=$upDestinationFolder+"/"+$ufFolderName
  41. # Returns a folder from a given site relative path, and will create it if it does not exist.
  42. Ensure-PnPFolder -SiteRelativePath $siteRelativePath
  43. }
  44. else
  45. {
  46. $siteRelativePath=$upDestinationFolder
  47. }
  48. write-host -ForegroundColor Yellow "Uploading files - Source Folder: " $ufSourceFolder " - Destination Folder: " $siteRelativePath
  49. # Loop through all the files
  50. foreach($file in Get-ChildItem $ufSourceFolder)
  51. {
  52. $filePath=$ufSourceFolder+"\"+$file.Name
  53. write-host -ForegroundColor Magenta "uploading the file.... " $file.Name
  54. # Add files to the respective folder
  55. Add-PnPFile -Path $filePath -Folder $siteRelativePath
  56. }
  57. }
  58. }
  59. ################################################################# Initiate #####################################
  60. function Initiate()
  61. {
  62. write-host -ForegroundColor Green "Initiating the script.................. "
  63. # Get Credentials and connect to SP Online site
  64. GetCredentials
  65. # Call the required functions
  66. UploadFiles
  67. # Disconnect from the server
  68. Disconnect-PnPOnline
  69. write-host -ForegroundColor Green "Completed!!!!"
  70. }
  71. #################################################################################################################
  72. Initiate
  73. Stop-Transcript

Create folders and add the files to the folder. See the folder structure for the reference.

Run Windows PowerShell as an administrator.

Navigate to the folder, where XML and ps1 files are available.

Type .\uploadfiles.ps1 and click Enter.



Result

The folders are created, which are based on the input provided in an XML file and the files are uploaded to the respective folder. In this blog, you saw how to upload the files in SharePoint Online, using PowerShell.

Reference

  • https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/EnsurePnPFolder.md
  • https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/AddPnPFile.md