4

I use a provider hosted Apps, so need to work with an App Web and Lists.

Could anybody provide a code snippet to get ClientContext() of the App?

Can I do something like this?

Uri appWeb = new Uri(HttpContext.Current.Request.QueryString["SPAppWebUrl"]);
using (var clientContext = TokenHelper.GetS2SClientContextWithWindowsIdentity(appWeb, HttpContext.Current.Request.LogonUserIdentity))
{
   ...
}
4

2 Answers 2

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

1

To get clientcontext for app web, you can check the code snippet below:

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

using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())
{
    Web web = clientContext.Web;
    clientContext.Load(web);
    clientContext.ExecuteQuery();

    ListCollection lists = web.Lists;
    clientContext.Load<ListCollection>(lists);

    clientContext.ExecuteQuery();
}

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.