I created a provider hosted app (asp.net MVC) and work with a database an so on. This App I deployed into my developer site to debug and test it.

Now I deploy a list declarativly by a feature. This list exist in the app domain


But when I try to Access the List with

TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, url.Authority);

It give me only the lists, that exists in the developer Dashboard (my.domain.com/Sites/developer).

The hostweb is set to the request parameter "SPHostUrl". The contextToken is from the the following call


How is it possible to get the List elements via ClientContext?

2 Answers 2


Sorry my mistake

I Used the following code

 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);

but the row

string hostWeb = System.Web.HttpContext.Current.Request["SPHostUrl"];

must be changed to

string hostWeb = System.Web.HttpContext.Current.Request["SPAppWebUrl"];

After that change, it work.


When you create a Provider Hosted SharePoint app, the code which is automatically generated refers to SPHostUrl. This is the Url of the Host web where an app is installed. At this time there is no instance of an app web since the project doesn't contain any SharePoint artifacts inside it. If you put a break point at the place where Query String Parameters are referred in the code you will see SPHostUrl as one of Query String parameters but there won't be any thing for App web. As soon as you add artifacts related to SharePoint to the project like List Definition, Content Type etc, a feature is added to the project and when you deploy it an instance of app web is created to host these artifacts and therefore SPAppWebUrl is also added to the Query String, so that you can now get reference to app web using Uri appWeb = new Uri(Request.QueryString["SPAppWebUrl"]);

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.