I have a default provider-hosted application that prints the site title.

When I publish it to my Windows Azure it works great, but when I publish it to my company server (Windows Server 2008 R2, IIS 7.5) I got this error:

Object reference not set to an instance of an object on TestPHWeb.TokenHelper.GetAccessToken(String refreshToken, String targetPrincipalName, String targetHost, String targetRealm) on TestPHWeb.SharePointAcsContext.RenewAccessTokenIfNeeded(Tuple2& accessToken, Func1 tokenRenewalHandler) on TestPHWeb.SharePointAcsContext.GetAccessTokenString(Tuple2& accessToken, Func1 tokenRenewalHandler) on TestPHWeb.Default.Page_Load(Object sender, EventArgs e)

My code:

var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

using (var clientContext = spContext.CreateUserClientContextForSPHost())
     clientContext.Load(clientContext.Web, web => web.Title);

I'm not using WebDeploy to publish it, I'm using 'File System' and copying files to my IIS folder.

It's a provider-hosted app for SharePoint Online written using VS2013

Someone knows what is happening?


I asked a question about this recently.

I believe that when the app is hosted in Azure/IIS that both sides of the protocol are in the cloud,

But when the app is hosted "on-prem", there is no easy way for both sides of the protocol to talk. But this case should be the same as running "localhost-hosted" where the local browser asks if you want to "trust localhost"?

Are you getting to this prompt? Does it ask you to trust your on-prem IIS server?

| improve this answer | |

I fixed this for me by adding the [SharePointContextFilter] attribute to my action method where I was trying to create the spContext.

    public ActionResult Search()
        Models.User user = GetUser();
        ViewBag.CurrentUser = user;
        var books = db.GetAllBooks();
        return View(books);

Didn't have enough rep to add a comment so adding this as an answer here.

| improve this answer | |

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.