I am new to developing provider hosted apps and going through some code snippets over the web to see how to get the context of the host web (or app web) to work on lists and libraries etc through the app. I read through the OAUTH concepts and the token flow for context token and accesstoken.

I saw in some of the code snippets that the client context is obtained using

var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);    
using (var clientContext = spContext.CreateUserClientContextForSPHost())

However at a few places it is retrieved using the contexttoken

var contextToken = TokenHelper.GetContextTokenFromRequest(Page.Request);
var hostWeb = Page.Request["SPHostUrl"];
using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))

So my question is, when are we required to work with context and access tokens in the code to get the client context. Or is it that both the methods mentioned above essentially do the same thing and any one can be used to get the clientcontext ?

1 Answer 1



You can get the context of host web and app web to use SharePoint lists and libraries from your tenant and app as well. Contexttoken is mostly used in low-trust provider hosted app. In low-trust provider hosted app, you have to use Azure to host your web application. Azure is providing SPAppToken in url, by getting that you can get authentication token for app. SPHostUrl and SPAppWebUrl are also provided in url. You can get that from url and use that urls to get context.

Here you can get some reference about basic SharePoint Provider hosted app.

Some Links for reference

  • I am working on provider hosted app, i found same issue of context is null. When i deploy app from visual studio app is working totally fine but when i try to add app in app catalog and then sometime it is giving error of context is null. Please help from last one week I am looking into this issue
    – Richa Jain
    Jun 12, 2017 at 14:20
  • My access token is not generating itself, after every 12-14 hrs app automatically stops working itself.
    – Richa Jain
    Jun 12, 2017 at 14:20
  • Are you using Provider hosted app with On-premise version? It may be self-signed certificate issues. Jun 13, 2017 at 4:06
  • No, I am using online sharepoint
    – Richa Jain
    Jun 13, 2017 at 11:16
  • I pre-assume that you are using Azure for web project hosting. Please check all steps related to deployment and development. You can get more idea by debugging your app or you can raise new question in sharepoint.stackexchange with proper description and screenshot. Jun 13, 2017 at 14:50

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.