Work with host web data using PnP JS library in the SharePoint Hosted Add-In

This article, will share you the steps to create a simple SharePoint Hosted add in using Napa tool with the help of PnP JavaScript Library in SharePoint online to access the data from Host web.

This article, will share you the steps to create a simple SharePoint Hosted add in using Napa tool with the help of PnP JavaScript Library in SharePoint online to access the data from Host web.

SharePoint Add-In is an independent component which is used to include a new or expands the functionality to the SharePoint. The SharePoint Add-In can access data and use components between two webs called Host web and Add-In web.

Host Web: A SharePoint site used to install the SharePoint Add-In is called Host web.

Add-In Web: A special child web site will be created during the installation of Add-In and all the components mentioned in Add-In are deployed to the web is called Add-In web.

The example from this article, uses a PnP JavaScript Library to access the Lists available in Host web and shows the Host web title from the SharePoint Online.

To access data from Add-In web, Work with add-in web data using PnP JS library in the SharePoint Hosted Add-In

Prerequisites:

  • Developer Site Collection
  • Napa Add-In tool or Visual Studio
  • PnP, fetch and e6-promise js files

Getting start with SharePoint Add In

  • Navigate to Developer Site Collection and then to the Site Contents page.
  • Click “Napa” Office 365 Development Tools link or Tile from Site Content Page. This will open a Napa tool in a new window.
  • The Napa tool, will lists out the existing created Add-Ins. From the same page click “Add New Project” tile.
Napa Add-In Page
Fig 1: Napa Add-In Page
  • That’ll redirect us to Add-In create page. There we have different options to select the Add-In type.
  • Select “SharePoint Add-in” type and enter project name as “pnpSharePointhostwebtest” for the add-in application in the “Project name” textbox. Then Click “Create” button.
Add In Create Page
Fig 2: Add-In code to retrieve data from Host web
  • In the Add-in application, select “Properties” icon to update properties to the Add-in.
  • Modify the project title as user friendly name, for example enter “PnP SharePoint Host Web Testing application”. Leave the other fields as default. Then click “Apply” button available on bottom of the page under left navigation.
Add-In General Properties
Fig 3: Add-In General Properties
  • We need to set the permissions to the add-in to access data from Host web. For that, In the properties page, select “Permissions” in left navigation.
  • In our example, we are going to read only web information and lists available from the web. So the Read permission is enough for the web object. So, set the Web permission to “Read”.
Add-In Permissions
Fig 4: Add-In Permissions
  • Remove the unrequired JavaScript file references from the Default.aspx page. In our example, we will remove the jquery, sp.js and sp.runtime js files.
Remove JavaScript references
Fig 5: Remove JavaScript references
  • Add the required files (pnp.min.js, fetch.js and es6-promise.js) to the Scripts folder.
Script Files
Fig 6: Script Files
  • After uploading the files, open Default.aspx page and add the below JavaScript file reference lines within PlaceHolderAdditionalPageHead content place holder.

[code language=”javascript”]

<script type="text/javascript" src="../scripts/fetch.js"></script>
<script type="text/javascript" src="../scripts/es6-promise.js"></script>
<script type="text/javascript" src="../scripts/pnp.min.js"></script>

[/code]

  • Now, open “App.js” file and remove all the lines and add the below lines,

[code language=”javascript”]

‘use strict’;

function pnpReady() {
var addinweb = getUrlParamByName("SPAppWebUrl");
var hostweb = getUrlParamByName("SPHostUrl");

//Retrieve Host Web Title and collection of Lists from the current web
$pnp.sp.crossDomainWeb(addinweb,hostweb).expand("Lists").select("Title,Lists").get().then(function(result) {
var contetTypeInfo = "<strong>Lists from website: " + result.Title + "</strong><br/>";
for (var i = 0; i < result.Lists.length; i++) {
contetTypeInfo += "Name: " + result.Lists[i].Title + "<br/>ID:" + result.Lists[i].Id + "<br/><br/>";
}
document.getElementById("message").innerHTML = contetTypeInfo;
}).catch(function(err) {
alert(err);
});
}

//Call the PnP method after the page load
document.addEventListener(‘DOMContentLoaded’, function() {
pnpReady();
});

//To retrieve the parameter value from query string in URL
function getUrlParamByName(name){
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
var results = regex.exec(location.search);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

[/code]

  • After adding the lines, the app.js file will look like as below,
Add-In code to retrieve data from Host web
Fig 7: Add-In Code

To use the same code in On-Premise environment, add the below snippet to ensure the response should have retrieved in JSON format.

[code language=”javascript”]
$pnp.setup({
headers: {
"Accept": "application/json; odata=verbose"
}
});

[/code]

The code in this example shows the Host Website’s Title and populates the lists (ID and Title) available in the web.

Output:

Host Web SharePoint Add-In Output
Fig 8: Host Web SharePoint Add-In Output

Conclusion:

From this article, we have learned on how to create a SharePoint add-in to access information from Host web using PnP JavaScript Library in SharePoint Online.

Shantha Kumar
Shantha Kumar
Articles: 278