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.
- <?xml version="1.0" encoding="utf-8"?>
- <Inputs>
- <ConnectSPOnline SiteURL="https://c986.sharepoint.com/sites/vijai" UserName="[email protected]" Password="xxxxxxxxx"></ConnectSPOnline>
- <UploadFiles>
- <UploadFile SourceFolder=".\HTML" DestinationFolder="SiteAssets" CreateFolder="yes" FolderName="HTML"></UploadFile>
- <UploadFile SourceFolder=".\CSS" DestinationFolder="SiteAssets" CreateFolder="yes" FolderName="CSS"></UploadFile>
- <UploadFile SourceFolder=".\Files" DestinationFolder="SiteAssets" CreateFolder="no" FolderName=""></UploadFile>
- </UploadFiles>
- </Inputs>
Open Notepad and paste the script given below. Save the text file as uploadfiles.ps1.
- ############################################################## Logging #########################################
-
- $date= Get-Date -format MMddyyyyHHmmss
- start-transcript -path .\Log_$date.doc
-
- ################################################### Get input parameters from XML ###############################
-
- # Get content from XML file
- [xml]$xmlData=Get-Content ".\Inputs.xml"
-
- # ConnectSPOnline node
- [System.Xml.XmlElement]$connectSPOnline = $xmlData.Inputs.ConnectSPOnline
- $siteURL=$connectSPOnline.SiteURL
- $userName=$connectSPOnline.UserName
- $password=$connectSPOnline.Password
-
- # UploadFiles node
- [System.Xml.XmlElement]$uploadFiles = $xmlData.Inputs.UploadFiles
-
- ########################################################## Get Credentials ######################################
-
- function GetCredentials()
- {
- write-host -ForegroundColor Green "Get Credentials and connect to SP Online site: " $siteURL
- # Convert password to secure string
- $secureStringPwd = ConvertTo-SecureString -AsPlainText $Password -Force
-
- # Get the credentials
- $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $userName,$secureStringPwd
-
- # Connect to SP online site
- Connect-PnPOnline –Url $siteURL –Credentials $credentials
- }
-
- ########################################################### Upload Files ########################################
-
- function UploadFiles()
- {
- write-host -ForegroundColor Green "Upload files to SP Online site"
-
- # Loop through UploadFiles XML node
- foreach($uploadFile in $uploadFiles.UploadFile)
- {
- # UploadFile node parameters
- $ufSourceFolder=$uploadFile.SourceFolder
- $upDestinationFolder=$uploadFile.DestinationFolder
- $ufCreateFolder=$uploadFile.CreateFolder
- $ufFolderName=$uploadFile.FolderName
-
- # Check whether folder has to be created
- if($ufCreateFolder="yes")
- {
- $siteRelativePath=$upDestinationFolder+"/"+$ufFolderName
-
- # Returns a folder from a given site relative path, and will create it if it does not exist.
- Ensure-PnPFolder -SiteRelativePath $siteRelativePath
- }
- else
- {
- $siteRelativePath=$upDestinationFolder
- }
-
- write-host -ForegroundColor Yellow "Uploading files - Source Folder: " $ufSourceFolder " - Destination Folder: " $siteRelativePath
-
- # Loop through all the files
- foreach($file in Get-ChildItem $ufSourceFolder)
- {
- $filePath=$ufSourceFolder+"\"+$file.Name
- write-host -ForegroundColor Magenta "uploading the file.... " $file.Name
-
- # Add files to the respective folder
- Add-PnPFile -Path $filePath -Folder $siteRelativePath
- }
- }
- }
-
- ################################################################# Initiate #####################################
-
- function Initiate()
- {
- write-host -ForegroundColor Green "Initiating the script.................. "
-
- # Get Credentials and connect to SP Online site
- GetCredentials
-
- # Call the required functions
- UploadFiles
-
- # Disconnect from the server
- Disconnect-PnPOnline
-
- write-host -ForegroundColor Green "Completed!!!!"
- }
-
- #################################################################################################################
-
- Initiate
- 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