I created a high trust provider trusted the app. I have some strange issue that I need to click on the retrust hyperlink after I deploy the app in SharePoint 2013 on premises. Can I do this programmatically in c# or Powershell? I would like to prevent this manual step.

enter image description here

  • 1
    what is the issue you are facing? "Click here to trust again" is by default shown even if there is no problem with the trust – Unnie Nov 7 '16 at 12:10
  • I would like to prevent clicking on this link manual. I would like to add it to my app installation powershell script. – Ola Nov 9 '16 at 12:40
  • Thanks for the link, but which line will do the (re)trust? – Ola Nov 9 '16 at 13:52
  • You can try it out bcoz retrust is essentially doing the trust again, so might work for you – Unnie Nov 9 '16 at 13:53

There is no out of the box solution to automate the trust of apps.
Indeed there are methods to trust an app programmatically, but as far I know not all scopes are available there.

We found a good solution by using an Internet Explorer instance on our deployment script, e.g. in PowerShell.

$authorizeURL = "$($WebUrl.TrimEnd('/'))/_layouts/15/appinv.aspx?AppInstanceId={$AppInstanceId}"

$ie = New-Object -com internetexplorer.application

$ie.Visible = $false

if ($ie.Document.Title -match "Do you trust.*") {
    sleep -seconds 5

    $button = $ie.Document.getElementById("ctl00_PlaceHolderMain_BtnAllow")

    if ($button -eq $null) {
        $button = $ie.Document.getElementById("ctl00_PlaceHolderMain_LnkRetrust")

    if ($button -eq $null) {
        throw "Could not find button to press"

        WaitFor-IEReady $ie

        #if the button press was successful, we should now be on the Site Settings page..
        if ($ie.Document.title -like "*trust*") {
            throw "Error: $($ie.Document.body.getElementsByClassName("ms-error").item().InnerText)"
            Write-Verbose "App was trusted successfully!"
    throw "Unexpected page '$($ie.LocationName)' was loaded. Please check your url."

The whole process (including Office365 login for SharePoint online if necessary) is documented in this blog.


Normally the process for distributing the app is following:

  1. Register new app principal.
  2. Update app package with valid client id.
  3. Configure environment (for on-premise setup S2S trust, certificates, etc.)
  4. Upload the app.
  5. Install the app to the web site.

When you are adding the app from the UI (from app catalog, developer site, or app store), SharePoint prompts you with "Trust" screen, so prior to installing, the app will be trusted.

Now you want to do that automatically as part of installation script.
I've never done that, but I'm aware of server side object model which can do that.

Assuming you are done step #1 and having clientid, you can do the following:

//load the principal manager
SPAppPrincipalManager manager = SPAppPrincipalManager.GetManager(targetWeb);
//Check if the app principal is registered in this farm using client ID
SPAppPrincipal appPrincipal = manager.LookupAppPrincipal(SPAppPrincipalIdentityProvider.External, SPAppPrincipalName.CreateFromAppPrincipalIdentifier(clientId));
//Trust the app
SPAppPrincipalPermissionsManager sPAppPrincipalPermissionsManager = new SPAppPrincipalPermissionsManager(targetWeb);
sPAppPrincipalPermissionsManager.AddAppPrincipalToWeb(appPrincipal, SPAppPrincipalPermissionKind.Read);  

A bit more info here SPAppPrincipalPermissionsManager
How to programmatically install an provider hosted app

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.