SharePoint Hosted App: Get Data from Host Web

This SharePoint apps or add-in tutorial, we will discuss how to get host web site title in SharePoint hosted add-in in SharePoint Online Office 365 Site. Also, we will discuss how to get all host web lists in add-in web in SharePoint Online Office 365 in SharePoint hosted add-in?

SharePoint hosted app get host web title or URL

Now, we will see how to get host web title or URL in SharePoint hosted add-in web in SharePoint Online Office 365.

We can give use the below function to retrieve the host web URL by using the query string 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 lines 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);

App.js Code:

'use strict';
ExecuteOrDelayUntilScriptLoaded(getHostSiteName, "sp.js");
var web;
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();
currentcontext.load(web, "Title");
currentcontext.executeQueryAsync(onSuccess, onFailure);
}

function onSuccess() {
$('#message').text("Host web title " + web.get_title());
}

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];
}
}
}

Default.aspx Code:

Below is the default.aspx code.

<%– 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 app get host web url

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

sharepoint hosted app get lists from host web

Now when the add-in will open then you can see all the host web title like below:

sharepoint get host web url

Get all host web lists in SharePoint hosted add-in

Now, we will see how to retrieve all lists from the host web and show it in the add-in web in SharePoint hosted add-in or apps in SharePoint Online Office 365.

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

We can give use the below function to retrieve the host web URL by using the query string 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 lines 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 app get host web url

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

sharepoint app get host web

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

sharepoint hosted app get data from host web

You may like following SharePoint hosted apps tutorials:

This SharePoint tutorial, we discussed how to get host web data in SharePoint hosted add-in or apps in SharePoint Online Office 365. Here, we saw how to retrieve a host web site title in SharePoint Online Office 365. And also, we saw how to get host web data like retrieve all lists and libraries in SharePoint hosted apps in SharePoint Online Office 365.

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

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

>