SharePoint 2013 Hosting – How to initialize the SharePoint Client Context in SharePoint Apps (Provider Hosted / SharePoint Hosted) using JSOM

Introduction

SharePoint Provider Hosted Apps First of all we need to understand the SharePoint Provider Hosted hosting blocks. Provider hosted involves two hosting domains (ex host, sphost). thus request sending to host to sphost considering as cross domain request. For cross domain request we need to provider

SharePoint Provider Hosted Apps

First of all we need to understand the SharePoint Provider Hosted hosting blocks.

image_thumb[5]

Provider hosted involves two hosting domains (ex host, sphost). thus request sending to host to sphost considering as cross domain request.
For cross domain request we need to provide SPAppWebUrl to initialize the context. SPAppWebUrl  is available in Url.

we can get AppWebUrl by accessing Url parameter. following shows a utility function for getting Url parameter

This will initiate the client context using remote web Url

SharePoint Hosted Apps

SharePoint Hosted app does not have separate hosting server. SharePoint hosted apps run on the same SharePoint server. thus client context initialization does not need to provide the Url.