We have a provider hosted app used by many customers in SharePoint Online, but there is this one new customer who is getting this error whenever he tries to launch the app after installation.

AppOnlyPolicy is true

User is tenant admin and site collection admin.

From exception stack trace, I suspect below piece of code is culprit:

Host context for Event receiver is fetched via:


host context for page request is fetched via:



this.SP.site = this.SP.hostContext.Site;
                this.SP.hostContext.Load(this.SP.site, s => s.ServerRelativeUrl, s => s.Id, s => s.Url);

                this.SP.rootWeb = this.SP.site.RootWeb;
                this.SP.hostContext.Load(this.SP.rootWeb, w => w.Id, w => w.Title, w => w.Url, w => w.ServerRelativeUrl, w => w.WebTemplate);

                this.SP.hostWeb = this.SP.hostContext.Web;
                this.SP.hostContext.Load(this.SP.hostWeb, w => w.Id, w => w.Title, w => w.Url, w => w.ServerRelativeUrl, w => w.WebTemplate);

                this.SP.hostContext.Load(this.SP.hostWeb.CurrentUser, user => user.Title, user => user.LoginName, user => user.Id, user => user.Email, user => user.IsSiteAdmin);
                this.SP.hostContext.Load(this.SP.hostWeb.RegionalSettings, r => r.LocaleId, r => r.Time24, r => r.TimeZone, r => r.DateFormat);



System.Net.WebException: The remote server returned an error: (401) Unauthorized.
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
   at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
   at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()

What all scenarios can be possible which can lead to this error, based on above info?

  • I have tried removing all instances of app doing fresh installation(including all recycle bins)
  • tried deleting and re-adding app principals (site collection + tenant level full control)
  • Installed the app via sharepoint store also tried via .app file in app catalog

As per logs I got this error at two point:

  • while app is getting installed (in app event receiver)
  • Whenever user tries to open any app pages

Both time same code is getting called

Your Answer

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

Browse other questions tagged or ask your own question.