Many architects are trying to establish a fully automated continuous deployment process for their SharePoint apps. While automating the build, packaging & deployment is fairly simple (check this), there is a big challenge that is preventing architects and developers from reaching the full automation stage. That’s is “Trusting the App“!
Following the deployment of the application components (to pre-prod environments), it is important to note that before anyone accessing the application, including automated tests that may be part of the build, a tenant (or site collection) administrator will have to trust the application on the app information page in SharePoint. This is usually a manual process and there is no way to trust your app through PowerShell or .NET CSOM but there is a trick that I use for achieving a full automation! PowerShell IE Automation!
Here is the script that I use from within my deployment script:
$url = “$($web.Url.TrimEnd(‘/’))/_layouts/15/appinv.aspx?AppInstanceId={$($appInstance.Id)}”
$ie = New-Object -com internetexplorer.application
try
{
$ie.visible=$true
$ie.navigate2($url)
while ($ie.busy)
{
sleep -milliseconds 60
}
$trustButton = $ie.Document.getElementById(“ctl00_PlaceHolderMain_BtnAllow”)
$trustButton.click()
sleep -Seconds 1
Write-Host “App was trusted successfully!”
}
catch
{
throw (“Error Trusting App”);
}
finally
{
$ie.Quit()
}
You just need to add the code above to your deployment script and VOILA. You no longer need to suspend the testing process until the application is trusted. Now a completely “Build-Deploy-Test workflow” is possible!
Finally, I would like to give some credit to Alexander Brassington, his blog post about automated publishing for content types using PowerShell IE Automation inspired me. Although, the post is completely unrelated to Build Automation, it helped a lot on coming up with the workaround!