none
Create client context issue with SharePoint provider hosted add-in RRS feed

  • Question

  • Hi,

    In my provider hosted add-in, I am creating the client context in every Index method in every Controller but not in Create and Edit methods. It is working fine but if any user copy the edit item url and try to access than it generates Client Context null error. My question is-

    1. Do I need to create client context in every Action methods every time? 
    2. Can I keep the client context in session variable on Session Start and use it in all other controller methods?
    3. How can I redirect user to login again when client session is time out?
    4. I am using [SharePointContextFilter] only on the Index method in every controller, do I need to use in other methods like- Create, Edit and so.

    Could anyone please help me out of this?

    Thursday, May 3, 2018 10:41 AM

Answers

  • Hi,

    We can create a ContextHelper class for use context in every Action method.

    using Microsoft.SharePoint.Client;
    using System;
     
    namespace Gobbe.Helpers
    {
        public class ContextHelper
        {
            public static ClientContext GetClientContext(Uri url)
            {
                string contextToken = TokenHelper.GetContextTokenFromRequest(System.Web.HttpContext.Current.Request);
                string hostWeb = System.Web.HttpContext.Current.Request["SPHostUrl"];
     
                if (!string.IsNullOrEmpty(contextToken))
                    System.Web.HttpContext.Current.Session["ctx"] = contextToken;
                else
                    contextToken = (string)System.Web.HttpContext.Current.Session["ctx"];
     
                if (!string.IsNullOrEmpty(hostWeb))
                    System.Web.HttpContext.Current.Session["host"] = hostWeb;
                else
                    hostWeb = (string)System.Web.HttpContext.Current.Session["host"];
     
                return TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, url.Authority);
            }
        }
    }
    

    More information is here:

    http://gobbe.net/2013/05/26/working-with-the-clientcontext-through-multiple-pages-in-a-sharepoint-cloud-hosted-app/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 7, 2018 9:25 AM
    Moderator

All replies

  • Hi,

    We can create a ContextHelper class for use context in every Action method.

    using Microsoft.SharePoint.Client;
    using System;
     
    namespace Gobbe.Helpers
    {
        public class ContextHelper
        {
            public static ClientContext GetClientContext(Uri url)
            {
                string contextToken = TokenHelper.GetContextTokenFromRequest(System.Web.HttpContext.Current.Request);
                string hostWeb = System.Web.HttpContext.Current.Request["SPHostUrl"];
     
                if (!string.IsNullOrEmpty(contextToken))
                    System.Web.HttpContext.Current.Session["ctx"] = contextToken;
                else
                    contextToken = (string)System.Web.HttpContext.Current.Session["ctx"];
     
                if (!string.IsNullOrEmpty(hostWeb))
                    System.Web.HttpContext.Current.Session["host"] = hostWeb;
                else
                    hostWeb = (string)System.Web.HttpContext.Current.Session["host"];
     
                return TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, url.Authority);
            }
        }
    }
    

    More information is here:

    http://gobbe.net/2013/05/26/working-with-the-clientcontext-through-multiple-pages-in-a-sharepoint-cloud-hosted-app/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, May 7, 2018 9:25 AM
    Moderator
  • Hi Dennis,

    Could you please guide me how can I prompt user to login again once the session is expired?

    Thanks.

    Thursday, May 10, 2018 11:01 AM
  • Hi,

    The First thing we have to do is store the Refresh token which is supplied in the post when SharePoint redirects the user to your app. I added this to the HttpContext Session in SharePointContext.cs.

    The next thing we need to do is to stop the page from redirecting. The SharePointContextFilter “checks the redirect status” via a method in SharePointContext CheckRedirectStatus, If it finds the stored SharePointContext to be expired it will redirect the user. I added a method in this function to check if there is a valid sphost url and a valid refresh token, if there is, do not redirect.

    Now that we have prevented it redirecting and stored the refresh token, we just have to check if the spContext is null, if it is, use the refreshtoken to create the clientcontext, if its not use the standard CreateUserClientContextForSPHost method to create.

    More information is here:

    http://www.sharepointpro.com.au/sharepoint-provider-hosted-app-redirects-after-timeout/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Friday, May 11, 2018 8:01 AM
    Moderator
  • Hi,

    Would you please provide us with an update on the status of your issue?

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnsf@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, May 22, 2018 9:07 AM
    Moderator
  • Hi Dennis,

    Sorry for late reply. This is a very good reference and solves most my issues except one. 

    I want the system will prompt for login again to re-authenticate if idle for 1/2 hours. If I understood the reference correctly, this part is not mentioned.

    Would you pleas explain?

    Thanks.

    Wednesday, May 23, 2018 10:52 AM