How to retrieve all host web lists in add-in web in SharePoint Online Office 365 in SharePoint hosted add-in?

SharePoint online retrieve host web data in add-in SharePoint hosted apps

Here we will discuss how we can retrieve all lists from host web and show it in add-in web in SharePoint hosted add-in in SharePoint Online Office 365. If you are new to the SharePoint hosted add-in, then you can check my below post on

how we can create a SharePoint hosted add-in using visual studio 2015.

SharePoint Online Develop SharePoint Hosted Add-in using Visual Studio 2015 Demo

In the above example, we have retrieved the logged in username. But here we will retrieve the host web lists.

Here we will first retrieve the host web URL and host context and then we will retrieve the host web lists.

Also, read:
Microsoft Flow Copy files from one SharePoint Online account or folder to another Office 365

Generate a .app file for SharePoint Apps using MSBuild and Visual Studio 2015

SharePoint 2016 error while removing Apps Sorry something went wrong. Please refresh the page and try again.

We can give use the below function to retrieve the host web url by using the querystring parameter.

function getURLParameters(param) {

var params = document.URL.split(‘?’)[1].split(‘&’);

var strParams = ”;

for (var i = 0; i < params.length; i = i + 1) {

var singleParam = params[i].split(‘=’);

if (singleParam[0] == param) {

return singleParam[1];

}

}

}

Then we can use the below 3 line to retrieve the host web context.

var hostUrl = decodeURIComponent(getURLParameters(“SPHostUrl”));

var currentcontext = new SP.ClientContext.get_current();

var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);

Here we have just put the below code in the App.js file.

‘use strict’;

ExecuteOrDelayUntilScriptLoaded(getHostSiteName, “sp.js”);

var web;

var collList;

function getHostSiteName() {

var hostUrl = decodeURIComponent(getURLParameters(“SPHostUrl”));

var currentcontext = new SP.ClientContext.get_current();

var hostcontext = new SP.AppContextSite(currentcontext, hostUrl);

web = hostcontext.get_web();

collList = web.get_lists();

currentcontext.load(collList);

currentcontext.executeQueryAsync(onSuccess, onFailure);

}

function onSuccess() {

var listInfo = ”;

var listEnumerator = collList.getEnumerator();

while (listEnumerator.moveNext()) {

var oList = listEnumerator.get_current();

listInfo += ‘Title: ‘ + oList.get_title() + ‘<br />’;

}

$(“#message”).html(listInfo);

}

function onFailure(sender, args) {

alert(‘Failed to get web title. Error:’ + args.get_message());

}

function getURLParameters(param) {

var params = document.URL.split(‘?’)[1].split(‘&’);

var strParams = ”;

for (var i = 0; i < params.length; i = i + 1) {

var singleParam = params[i].split(‘=’);

if (singleParam[0] == param) {

return singleParam[1];

}

}

}

The default.aspx page code is like below:

<%– The following 4 lines are ASP.NET directives needed when using SharePoint components –%>

<%@ Page Inherits=”Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” MasterPageFile=”~masterurl/default.master” Language=”C#” %>

<%@ Register TagPrefix=”Utilities” Namespace=”Microsoft.SharePoint.Utilities” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%@ Register TagPrefix=”WebPartPages” Namespace=”Microsoft.SharePoint.WebPartPages” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%@ Register TagPrefix=”SharePoint” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

<%– The markup and script in the following Content element will be placed in the <head> of the page –%>

<asp:Content ContentPlaceHolderID=”PlaceHolderAdditionalPageHead” runat=”server”>

<script type=”text/javascript” src=”../Scripts/jquery-1.9.1.min.js”></script>

<SharePoint:ScriptLink name=”sp.js” runat=”server” OnDemand=”true” LoadAfterUI=”true” Localizable=”false” />

<meta name=”WebPartPageExpansion” content=”full” />

<!– Add your CSS styles to the following file –>

<link rel=”Stylesheet” type=”text/css” href=”../Content/App.css” />

<!– Add your JavaScript to the following file –>

<script type=”text/javascript” src=”../Scripts/App.js”></script>

</asp:Content>

<%– The markup in the following Content element will be placed in the TitleArea of the page –%>

<asp:Content ContentPlaceHolderID=”PlaceHolderPageTitleInTitleArea” runat=”server”>

Page Title

</asp:Content>

<%– The markup and script in the following Content element will be placed in the <body> of the page –%>

<asp:Content ContentPlaceHolderID=”PlaceHolderMain” runat=”server”>

<div>

<p id=”message”>

<!– The following content will be replaced with the user name when you run the app – see App.js –>

initializing…

</p>

</div>

</asp:Content>

Apart from that here we need to have read permission for the site collection. So we can open the AppManifest.xml file and go to the Permissions tab and you can give read access to the Site Collection like below:

SharePoint hosted add-in retrieve host web data in add-in web
SharePoint hosted add-in retrieve host web data in add-in web

Once you right click on the project and click on deploy. It will ask you to trust the add-in like below:

retrieve host web data in add-in web SharePoint hosted apps
retrieve host web data in add-in web SharePoint hosted apps

Now when the add-in will open then you can see all the host web lists in the add-in.

SharePoint online retrieve host web data in add-in SharePoint hosted apps
SharePoint online retrieve host web data in add-in SharePoint hosted apps

Hope this will be helpful.


You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →