Sharepoint 2013 - How To Access Host Web From Sharepoint App/Add-In

In this article, I will discuss another important implementation,  which shows how we can access Host Web elements from with the App Executing (App Web) within Host Web.

To perform this demo, we would follow a use case, where the user of my App needs to access the data from the lists present in the Hosting Web or may be saving their settings information into the Hosting Web to support Backup/Restore features.

Consider a list “Categories”, which is present in Hosting Web Development.

1

If we look inside Categories list, we can see the data corresponding to each category.

2

Thus, our Hosting Web is ready with the data. Now, it is the time to start with developing a new app, which will be hosted by our Hosting Web.

  • Launch Visual Studio.
  • Select App for SharePoint template to create a new App Project.
  • Give the project; a suitable name.
  • Click OK.

3

  • Since we are developing SharePoint Hosted app, select “SharePoint-Hosted” as the hosting model.
  • Click Finish.

4

Wait till Visual Studio configures the app project for you.

5

Once the project is ready, we can see the default.aspx, which is launched by default.

6

On inspecting Solution Explorer, we can see all the artifacts, which are part of this project.

Out of all “App.js” and “App.css” under Script and Content directories respectively, it deserves special attention.

  • “App.js” is the JS file, where we are going to put all our business logic.
  • “App.css” is the CSS file, where we are going to put all our business specific branding.

7

In Step 1, we can see SharePoint JS Libraries, which are added by default to the project by SharePoint App Project Template.

In Step 2, we can see the reference of “App.css” file added to the default.aspx by default.

In Step 3, we can see the reference of “App.js” file added to the default.aspx by default.

Thus, we are all set to focus on our business logic directly. All thanks to SharePoint app project templates. 

8

Let’s start with default.aspx to add some UI elements to showcase an intuitive UI to our end users.

In Step 1 & 2, we are adding HTML & CSS to default.aspx.

In Step 3, we are adding a HTML button and applying CSS to it. We will be hooking up the click event to this button later on in our JS file.

9

Once we got the UI ready, we can deploy the app by clicking Start button in Visual Studio Toolbar.

10

It is worth to look for an output Window to watch out the progress of the deployment process.

11

Provide the credentials, when you are asked.

12

Here, it is default.aspx page of our app.

Step 1

It represents the app URL in action under a separate app domain.

Step2

It represents the results panel, where we will show the output of an app logic execution.

Step 3

It represents HTML button element, which will trigger the business logic execution.

13

Now, let’s add the code to “App.js” file.

In Step 1, we are hooking up the Click Event Handler to our HTML button. The handler function “callToHostWeb” will get execute on button click.

In Step 2, we are reading a query string token “SPHostUrl” and this token will provide us with the Host Web URL.

In Step 3, we are calling “AppContextSite” method by passing the “App Context” and “Host Web URL “ to initialize the “Host Web Context”.

In Step 4, 5, 6, we are performing usual JSOM operations of reading List Data from Host Web.

In Step 7, we are building a message to display in the Result Panel of our UI only for the previous operations in steps 1-6, which have been completed successfully.

14

Step 8

It represents the helper method to retrieve the query string parameters out of the requested URL.

15

Once we are done with the code, we can deploy the app again and see what happens.

16
Oops, we got an exception.

Why does this exception come now and not earlier?

This is because, this time we are trying to access the Host Web for which the app does not have the required permissions.

In order to fix this issue, we need to grant at least Read permission to the Host Web.

Grant app permissions
  • Go back to Visual Studio.
  • Locate the “AppManifest.xml” in Solution Explorer.

    17
  • Click Permissions tab.

    18
  • Choose Site Collection from Scope dropdown and Read from Permission dropdown.

    19

Now, deploy the app again

Click Trust It, when it is asked.
20
Now, click Call to Host Web button to get all the category names read from Categories List from the Host Web.

21

  • We can also verify the deployment by navigating to the Host Web in “Apps in Testing” Library.

    22

Based on this simple walkthrough, we can conclude how simple it is to interact with the Host Webs from within the SharePoint apps (Add-ins).

You will find this a very common requirement to communicate for an app with its hosting Web for one or the other reasons. Now, you know how simple it is to deal with it.

I hope you found it helpful.