Skip to content

Instantly share code, notes, and snippets.

@vman
Last active November 14, 2015 11:11
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll"
#Utility function to authenticate CSOM and return the Client Context
Function Authenticate-CSOM($siteUrl, $adminUsername, $secureAdminPassword) {
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($adminUsername, $secureAdminPassword)
$ctx.Credentials = $credentials
return $ctx
}
Function InstallOrUpdateSolution($SiteUrl , $wspFullFilePath, $WspFileName, $wspPackageName, $majorVersion, $minorVersion, $scAdminUsername, $secureAdminPassword){
$ctx = Authenticate-CSOM $SiteUrl $scAdminUsername $secureAdminPassword
#Upload wsp to Solution Gallery
Write-Host "Uploading file to Solution Gallery"
$solutionGallery = $ctx.Web.Lists.GetByTitle("Solution Gallery")
$solutionGalleryRootFolder = $solutionGallery.RootFolder
$ctx.Load($solutionGallery)
$ctx.Load($solutionGallery.RootFolder)
$ctx.ExecuteQuery()
$wspfileStream = New-Object System.IO.FileStream($wspFullFilePath, [System.IO.FileMode]::Open)
#Create the FileCreationInformation object and prepare to upload it to the solution gallery
$fileCI = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$fileCI.Overwrite = $true
$fileCI.ContentStream = $wspfileStream
$fileCI.URL = $WspFileName
#upload the solution to the gallery
$uploadedFile = $solutionGallery.RootFolder.Files.Add($fileCI);
$ctx.Load($uploadedFile);
$ctx.ExecuteQuery();
Write-Host "done"
#Install the solution
Write-Host "Installing solution"
#Create the DesignPackageInfo object
$wsp = New-Object Microsoft.SharePoint.Client.Publishing.DesignPackageInfo
$wsp.PackageGuid = [System.Guid]::Empty
$wsp.PackageName = $wspPackageName
$wsp.MajorVersion = $majorVersion
$wsp.MinorVersion = $minorVersion
#Install the solution from the file url
$filerelativeurl = $solutionGallery.RootFolder.ServerRelativeUrl + "/" + $WspFileName;
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Install($ctx, $ctx.Site, $wsp, $filerelativeurl)
$ctx.ExecuteQuery()
Write-Host "done"
#Apply/Activate the solution
Write-Host "Applying solution"
[Microsoft.SharePoint.Client.Publishing.DesignPackage]::Apply($ctx, $ctx.Site, $wsp);
$ctx.ExecuteQuery()
Write-Host "done"
#The Install and Apply methods create a copy of the solution. This copy is renamed according to the Major and Minor Version specified in the DesignPackageInfo object.
#Once this copy is activated, we no longer need the original wsp file uploaded to the solution gallery.
Write-Host "Deleting temporary files"
$uploadedSolutionFile = $solutionGallery.rootFolder.Files.GetByUrl($filerelativeurl);
$uploadedSolutionFile.DeleteObject();
$ctx.ExecuteQuery()
Write-Host "done"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment