How to access Host Web data in App Web (SharePoint Add-in)

In this blog we will know that how to access data of Host Web in to the App web. In SharePoint add-in has its own app domain which is very secure. To access the outside data (from Host web or Tenant) in the SharePoint add-in is not permitted by the app domain. We cannot use the SP list data in add-in directly if list does not exist in the app domain. To create a connection between the two add-in or add-in and Host Web SharePoint list required the tenant permission in the app to access the tenant data and target should be host web url. Let’s have a look that how to achieve it by setting permission and code.

  • Permission: This is the first step which is very import part to assign permission to tenant. Without tenant permission any type of code will not work. To achieve this open app manifest, choose Permission tab and select Tenant in scope and set write permission to that and give read permission to Site Collection.
AppMenifest

With the help of this setting we can access the data of any list which is outside of the app domain or exists in the tenant and also we can perform manipulation on the data in host site lists from SharePoint add-in.

  • Example: Now in second step we will see the code syntax which is required to access data.

REST API Syntax:

appWebUrl/_api/SP.AppContextSite(@t)/web?@t=’targetUrl’

 

Full URL Example:

http://xxxx-5334ef4b86c8ea.sharepointonline.com/apps/_api/SP.AppContextSite(@t)/web?@t=’http://xxxx.sharepointonline.com/anothersite’

 

Example:

{

url:appweburl+api/SP.AppContextSite(@target)/web/lists/getbyTitle(‘list name‘)/Items?$select=Title

&@target='” + hostweburl + “‘”,

method: “GET”,

headers: { “Accept”: “application/json; odata=verbose” },

success: function (data) {

alert(“success: ” + JSON.stringify(data));

},

error: function (err) {

alert(“error: ” + JSON.stringify(err));

}

}

With the help of above steps data can be exchange very easily between host web to app web.

Related Posts

Leave a comment