SharePoint Online Site: How to retrieve host web site title in add-in web in SPhosted add-in?

Introduction:

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

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:

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/sharepoint_hosted_add-in_get_host_web_data-1.png

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

https://www.enjoysharepoint.com/wp-content/uploads/2018/09/get-host-web-data-sharepoint-hosted-addin-sharepoint-online-1.png

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

https://www.enjoysharepoint.com/wp-content/uploads/2018/07/SharePoint_hosted_addin_host_web_office_365.png

References

You can read below useful articles:

Conclusion:

Here we have discussed how we can retrieve host web site title inside SharePoint hosted add-in in SharePoint online Office 365.