I have a SharePoint Hosted App that I want to deploy to a SPOnline site and trust the App programmatically using CSOM. Idea is to mimic manual installation completely. I tried the below threads and the App starts appearing in the site content, however, it is not trusted. The CSOM code is running with credentials of a Tenant Administrator and App requires manage permissions on the site:

How to add a custom app to a SharePoint Online site programmatically

Deploying and Installing Sharepoint apps into App catalog using CSOM

There is a remote event receiver for App Install event and it is getting triggered:

using (ClientContext clientContext = tokenObj.CreateAppEventClientContext(properties, useAppWeb: false))
                if (clientContext != null)

However, last line throws exception:

ServerUnauthorizedAccessException: Access denied. You do not have permission to perform this action or access this resource.

Please let me know if it is possible in SPOnline to install an App and trust it so that it is fully usable as manually installed App.

Update 1: Changed App manifest to remove remote event receiver endpoint for AppInstalled event. Now the App gets installed but it still doesn't have permissions. If I trust the App again manually from the App permissions page then everything starts working.

2 Answers 2


After spending considerable time on this, I am concluding that App can be installed using LoadAndInstallApp method, but it has to be trusted manually only.

My remote event receiver for App Installed event was throwing exception as it was trying to perform operation on host web using the context, however, since App is not trusted it was failing. After commenting out this code App installed successfully, but App still didn't have permissions.

I looked at AppInstance object returned by LoadAndInstallApp and tried few other things but none of that gives permissions to App automatically. Tried the App Install with Tenant Admin user context so user permissions is not an issue here.



I answered this with a workaround in powershell How do I set a SharePoint-hosted app's permissions via PowerShell? You can try refactor it so it might help your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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