How to retrieve host web site title in add-in web in SharePoint hosted add-in in SharePoint Online Office 365 Site?

retrieve host web site title in add-in web in SharePoint hosted add-in

In this post, we will discuss how we can retrieve host website title inside add-in in SharePoint hosted add-in inside SharePoint online Office 365 site. If you are new to the SharePoint hosted add-in, you can read the below article on


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

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);

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 Permssions tab and you can give read access to the Site Collection like below:

retrieve host web site title in add-in web in SharePoint hosted add-in in SharePoint Online
retrieve host website title in add-in web in SharePoint hosted add-in in SharePoint Online

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

SharePoint hosted add-in get hosted add-in web site
SharePoint hosted add-in get hosted add-in web site

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

retrieve host web site title in add-in web in SharePoint hosted add-in
retrieve host website title in add-in web in SharePoint hosted add-in

Also you can read below articles:
Microsoft Flow Copy files from one SharePoint Online account or folder to another Office 365

SharePoint 2013 Online Content Search WebPart with Query String


Rest API filter list items created by logged in user SharePoint online or SharePoint 2013

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 →