What are the different ways to build Client Context in SharePoint provider-hosted add-in?

SharePoint provider-host

There are multiple ways to create Client Context in Provider-hosted add-in. Some methods are already present in SharePointContext.cs class and few we have to build explicitly as shown below:

  1. Build client context using User access for Host Web
  2. Build client context using User access for App/Add-in Web
  3. Build client context using App access for Host Web
  4. Build client context using App access for App/Add-in Web
  5. Build client context using User access for given Web
  6. Build client context using App access for given Web

In order to create Client Context, first we have to build SharePoint context which contains information like spHostUrl, spAppWebUrl, spLanguage, spClientTag, spProductNumber, userAccessTokenForSPHost, userAccessTokenForSPAppWeb, appOnlyAccessTokenForSPHost and appOnlyAccessTokenForSPAppWeb. The URL’s, access tokens, and few other information present in SharePoint context object is used to build client context.

Please find the below code to create/build SharePoint context object:

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

In the above line, HttpContext gets the HTTP-specific information about an individual HTTP request which contains information like Host web URL, App/Add-in web URL, etc. This information is helpful in building SharePoint context.

Now let’s discuss how to build Client context using SharePoint context.

  • Build client context using User access for Host Web : Below code builds the client context instance to the host web using current logged-in user privileges.
using (var clientContext = spContext.CreateUserClientContextForSPHost())

{

if (clientContext != null)

{

//write your client calls here to SharePoint to do your operations

}

}
  • Build client context using User access for App/Add-in Web
Below code builds the client context instance to the app/add-in web using current logged-in user privileges.

using (var clientContext = spContext.CreateUserClientContextForSPAppWeb())

{

if (clientContext != null)

{

//write your client calls here to SharePoint to do your operations

}

}
  • Build client context using App access for Host Web

Below code builds the client context instance to the host web using SharePoint app privileges.

Below code builds the client context instance to the host web using SharePoint app privileges.

Using (var clientContext = spContext.CreateAppOnlyClientContextForSPHost())

{

if (clientContext != null)

{

//write your client calls here to SharePoint to do your operations

}

}
  • Build client context using App access for App/Add-in Web

Below code builds the client context instance to the app/add-in web using SharePoint app privileges.

using (var clientContext = spContext.CreateAppOnlyClientContextForSPAppWeb())

       {

          if (clientContext != null)

          {

            //write your client calls here to SharePoint to do your operations

          }

}
  • Build client context using App access for given Web

Below code builds the client context instance to the given web using SharePoint app privileges. We need to provide SharePoint site/subsite URL in which you want to access/build client context

var uri = new Uri(webUrl);

var accessToken = TokenHelper.GetAppOnlyAccessToken( TokenHelper.SharePointPrincipal,uri.Authority, TokenHelper.GetRealmFromTargetUrl(uri));

using (var clientContext = TokenHelper.GetClientContextWithAccessToken( uri.ToString(), accessToken.AccessToken))      

{

          if (clientContext != null)

          {

            //write your client calls here to SharePoint to do your operations

          }

}

Below code builds the client context instance to the given web using SharePoint app privileges. We need to provide SharePoint site/subsite URL in which you want to access/build client context.Build client context using User access for given Web

i. If you have refresh token, then you can make use of below code to build client context instance.

var uri = new Uri(webUrl);

var accessToken = TokenHelper.GetAccessToken(“app refresh token”, TokenHelper.SharePointPrincipal, uri.Authority, TokenHelper.GetRealmFromTargetUrl(uri));

using (var clientContext = TokenHelper.GetClientContextWithAccessToken( uri.ToString(), accessToken.AccessToken))

{

if (clientContext != null)

{

//write your client calls here to SharePoint to do your operations

}

}

ii If you have authorization code, then you can use below code to build client context instance.

var uri = new Uri(webUrl);

var accessToken = TokenHelper.GetAccessToken(“authorization code”, TokenHelper.SharePointPrincipal, uri.Authority, TokenHelper.GetRealmFromTargetUrl(uri));

using (var clientContext = TokenHelper.GetClientContextWithAccessToken( uri.ToString(), accessToken.AccessToken))      

{

   if (clientContext != null)

          {

            //write your client calls here to SharePoint to do your operations

          }

}

However there are few other methods also available for creating client context,  apart from one which are described here.

Important points about SharePoint provider-hosted app/add-in
  1. Client ID
    • SharePoint add-ins should follow lower case strictly.
    • You can associate only one client ID per add-in.
  1. Client secret
    • You can associate multiple client secrets with single client ID.
    • For security and administrative purpose, Microsoft recommends to limit the number of client secrets per client ID.
    • Client secrets for SharePoint add-ins that are registered using the AppRegNew.aspx page expire after one year.
    • You can also set variable expiry time ranging from few months to maximum 3 years.
    • From Seller Dashboard, we can configure its expiry date max to 3 years directly from the browser, but if you have registered SharePoint add-in using AppRegNew.aspx page then you have to follow this msdn article to increase/replace add-ins expiry date.
  2. Access tokens for add-ins will expire after 24 hours.
  3. Refresh Tokens for add-ins are valid only for 6 months.
  4. You can register SharePoint add-in on SharePoint site from Seller dashboard or using AppRegNew.aspx page. Please find the AppRegNew.aspx page URL below:

http:///_layouts/15/AppRegNew.aspx

  1. To view registration details of any registered add-in navigate to

http:///_layouts/15/AppInv.aspx

  1. To see a list of registered add-in principals, navigate to:

http:///_layouts/15/AppPrincipals.aspx

  1. Once you register your SharePoint add-in on SharePoint site, you must copy and store the Client secret of add-in in a secured place. You can’t get the client secret of an add-in if you lost it. However, there is a possibility to generate new client secret in case of secret lost.

Related posts