none
SharePoint provider hosted app context token is null in Client Web Part RRS feed

  • Question



  • I have a working SharePoint provider hosted app, with an MVC web application residing on an Azure Web site. Entering the full screen app page, everything looks as expected.

    Adding a client web part or manually adding an iframe via a Script Editor web part, pointing at the exact same web application, results in no context token returned from the TokenHelper.

    I have the following method in an MVC controller:

    public ActionResult Index()
    {
        var contextToken = TokenHelper.GetContextTokenFromRequest(Request);

        // store the context token to use subsequently    

        return View();
    }

    Further investigation reveals that the TokenHelper.GetContextTokenFromRequest returns null as the request form does not contain the "SPAppToken", when called from the iframe.

    Can someone shed some light on the difference between the fullscreen app and the app hosted in an iframe/client web part?
    Saturday, December 20, 2014 1:08 AM

Answers




  • You should probably just use the new SharePointContext helper that was added in VS2013. It abstracts out many of the TokenHelper nuances that were somewhat annoying to deal with -- getting a token if one isn't present, using refresh tokens to get access tokens, saving the token across multiple pages in your app... etc.

    That's just advice in general, but specific to your question the SharePointContext helper will understand how to go get a new token if your ClientWebPart wasn't given one as part of the initial request.

    In your ASP.NET project, you should see a SharePointContext.cs file that has the code for the context helper.

    Here is a brief example of using the context helper instead of the token helper from a good SP Blog article on MSDN: http://blogs.msdn.com/b/officeapps/archive/2013/11/07/announcing-the-new-sharepointcontext-helper-in-apps-for-sharepoint-2013.aspx

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

    using (var clientContext = spContext.CreateUserClientContextForSPHost())
    {
        if (clientContext != null)
        {
            User spUser = clientContext.Web.CurrentUser;
            clientContext.Load(spUser, user => user.Title);
            clientContext.ExecuteQuery();
        }
    }

    Something else that applies either way (TokenHelper or Context Helper) -- you should make sure you're passing the {StandardTokens} tokens to all of your pages -- you have to set this for your ClientWebPart seperately in the App manifest.The context helper depends on the URLs provided in the {StandardTokens} to figure out how to create the right access tokens.
    • Marked as answer by TKDDOA Saturday, December 20, 2014 1:16 PM
    Saturday, December 20, 2014 8:41 AM