This article is about how I was able to use the SharePoint Modules to successfully deploy an application to SharePoint Online.
I’ve used some scripting before to update items in Sharepoint. This blog article is how I took the build that IOZ tools creates and deploy it to Sharepoint Online.
First I needed to start with downloading the latest copy of SharePointPnPPowerShellOnline.
I discovered while using this module that there is the capability to add a PSDrive to my session. This means I should be able to upload files to sharepoint as if it were a drive on my local machine. Here is how you connect to sharpeoint online:
PS> Install-Module -Name SharePointPnPPowerShellOnline
$adminpassword = 'password'
$adminUserName = 'mysharepoint@onmicrosoft.com'
$creds = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force
$SPdevcredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminUserName, $creds
connect-pnponline -Url $url -Credentials $SPdevcredentials -CreateDrive
Now that I have a connection to the SharePoint online instance I can see that I have a new powershell drive through the get-psdrive cmdlet:
get-psdrive
As you can see I have a new drive that is configured for use in my session, directorying the SPO: drive will get the contents of the SharePoint Site:
My Applications are in my AppCatalog folder to get to that folder all I need to do is issue a CD to that directory. To upload my App to this folder all I need to do is add it with Add-pnpFile.
I seemed to have the best success when I used Get-Item (gi) and then used the fullname property for the file that I was sending to SharePoint. In addition one other gotcha was that the folder to upload to is a subfolder of the site you are connected to. In my case \sites\apps was my site I was connected to so specifying appcatalog was all I needed.
Now all that I needed to do was to put this in a script that I could call from my CI automation and put some Error logic. Now I have a full fledged script called deployspapp.ps1. Full Source is found on my Gist:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param | |
( | |
[String] | |
[Parameter(Mandatory)] | |
$SourceFolder, | |
[String] | |
$Destination , | |
[string] | |
$AdminUserName, | |
[string] | |
$AdminPassword | |
) | |
$creds = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force | |
$SPdevcredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AdminUserName, $creds | |
#invoke-webrequest -uri $url -Credential $SPdevcredentials | |
if($MyInvocation.MyCommand.Path) | |
{ | |
$scriptpath = $MyInvocation.MyCommand.Path | |
$d = Split-Path $scriptpath | |
write-output -InputObject "Script Directory -->$D" | |
pushd | |
cd $d | |
import-module .\SharePointPnPPowerShellOnline -DisableNameChecking -Force | |
popd | |
} | |
else{import-module .\SharePointPnPPowerShellOnline -DisableNameChecking -Force} | |
$u = $null | |
$isurl = [system.uri]::TryCreate($Destination,[system.urikind]::RelativeOrAbsolute,[ref]$u) | |
if(($U.Scheme -eq 'http' ) -or ($u.Scheme -eq 'https')) | |
{ | |
if(get-command connect-pnponline -erroraction ignore) | |
{ | |
$url = $u.AbsoluteUri | |
do #for($i = 1; $i -le $u.Segments.count; $i++) | |
{ | |
try | |
{ | |
connect-pnponline -Url $url -Credentials $SPdevcredentials -CreateDrive -ErrorAction Ignore | |
} | |
catch | |
{ | |
"Error was $_" | |
$line = $_.InvocationInfo.ScriptLineNumber | |
"Error was in Line $line" | |
} | |
$i++ | |
$url = $U.AbsoluteUri -replace (($u.segments | select -last $i ) -join ''), '' | |
}While((-not (get-psdrive -name spo -ErrorAction Ignore)) -and ($i -ne $u.segments.count) ) | |
if(Get-PSDrive -Name spo -ErrorAction Ignore) | |
{ | |
Write-Output "Drives = $((Get-PSDrive).name -join ',')" | |
Write-Output "Site root = $((get-pnpsite).url)" | |
if ($sourcefolder.EndsWith('\')){$sourcefolder = $sourcefolder.trimend('\')} | |
$apps= Get-ChildItem -Path "$SourceFolder" | |
Write-Output "Application to Deploy Filename: $($apps.fullname)" | |
foreach($app in $apps) | |
{ | |
pushd | |
cd ($app ).directory | |
Add-PnPFile -Path $app.fullname -Folder ($Destination -replace (get-pnpsite).url, '') | |
write-output "uploaded $($app.fullname) to $destination" | |
popd | |
} | |
} | |
else | |
{ Throw "Cannot Connect to Sharepoint Online - $destination check username / password / url"} | |
} | |
} | |
else | |
{ | |
throw "$destination url must be an http or https address" | |
} |
I hope this helped someone
Until then keep scripting
Thom