Skip to main content

Adding master page to Auto-hosted apps

When we build a SharePoint hosted app, all pages and other resources are served by SharePoint from within the app web. For this reason, SharePoint hosted apps automatically inherit the look and feel of the host web and present custom functionality within the familiar SharePoint environment.

This inheritance is not automatic when we create a remote hosted app because the remote web is served from outside the SharePoint environment. There is no requirement to ensure that your remote web looks like the host web but a similar look and feel reassures users, positions familiar links and controls where users expect them, and can ease the adoption of your apptime.

Steps to add host web master page to app pages


1.     To use the Chrome Control, copy the sp.ui.controls.js JavaScript library into your remote web project.

2.     Ensure each page in app includes <script> element to the above JavaScript file.
<script        type="text/javascript"        src="../Scripts/sp.ui.controls.js">    </script>

3.     Add ChromeLoader.js to appweb project and provide the reference to each page in app
<script        type="text/javascript"        src="../ChromeLoader.js">    </script>


4.     Add following div element to .aspx page
  <div id="chrome_ctrl_placeholder"></div>
  

Best Practice: 


Use the Chrome Control whenever you want your app to blend with the host web's look and feel.
You cannot be sure that the host web will always use the familiar SharePoint look and feel so, if you hard-code the usual SharePoint design into your app, the app may not look like the customer's host web. The Chrome Control avoids this problem by downloading the style sheet and headers elements from the host web at run time.


Code: ChromeLoader.js

var hostweburl;

//load the SharePoint resources
$(document).ready(function () {
    //Get the URI decoded URL.
    hostweburl =
        decodeURIComponent(
            getQueryStringParameter("SPHostUrl")
    );

    // The SharePoint js files URL are in the form:
    // web_url/_layouts/15/resource
    var scriptbase = hostweburl + "/_layouts/15/";

    // Load the js file and continue to the
    //   success handler
    $.getScript(scriptbase + "SP.UI.Controls.js", renderChrome)
});

//Function to prepare the options and render the control
function renderChrome() {

    // The Help, Account and Contact pages receive the
    //   same query string parameters as the main page
    var options = {
        "appIconUrl": "siteicon.png",
        "appTitle": "My Reports",      
        //"settingsLinks": [
        //    {
        //        "linkUrl": "Account.html?"
        //            + document.URL.split("?")[1],
        //        "displayName": "Account settings"
        //    },
        //    {
        //        "linkUrl": "Contact.html?"
        //            + document.URL.split("?")[1],
        //        "displayName": "Contact us"
        //    }
        //]
    };

    var nav = new SP.UI.Controls.Navigation(
                            "chrome_ctrl_placeholder",
                            options
                        );
    nav.setVisible(true);
}

// Function to retrieve a query string value.
// For production purposes you may want to use
//  a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
    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] == paramToRetrieve)
            return singleParam[1];
    }
}


HTML Page:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AppWeb.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Home</title>
      <script
        src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js"
        type="text/javascript">
    </script>
    <script
        type="text/javascript"
        src="../Scripts/jquery-1.9.1.min.js">
    </script>     
      <script
        type="text/javascript"
        src="../Scripts/sp.ui.controls.js">
    </script>
    <script
        type="text/javascript"
        src="../ChromeLoader.js">
    </script>
   
</head>
<body>
    <form id="form1" runat="server">
         <div id="chrome_ctrl_placeholder"></div>

    <!-- The chrome control also makes the SharePoint
          Website stylesheet available to your page -->
        <div style="padding-left:100px">
    <h1 class="ms-accentText">My Reports</h1>
    <h2 class="ms-accentText"> </h2>
    <div id="MainContent">       
    <div>
        <h2>  </h2>
    <hr />
        <div>
       
    </div></div></div>
    </form>
</body>

Comments

Popular posts from this blog

GPT-5.2 Lands in Microsoft 365 Copilot: Smarter Reasoning for Workflows

 Today Microsoft announced that GPT-5.2 is now available in Microsoft 365 Copilot and Copilot Studio , bringing two tuned variants: GPT-5.2 Thinking for deep reasoning and strategic work, and GPT-5.2 Instant for fast writing, translation, and skill-building. In Copilot, the model connects to Work IQ to surface insights across your meetings, emails, and documents—helping you turn scattered information into actionable plans. If you use Copilot Chat or Copilot Studio, you can pick GPT-5.2 from the model selector and try prompts like asking for strategic takeaways from a meeting, building comparative tables of market leaders, or preparing focused talking points for upcoming conversations. Microsoft emphasizes model choice , so teams can select the model that best fits the task while keeping security, compliance, and privacy controls in place. Rollout starts today 11-Dec-2025 for users with a Microsoft 365 Copilot license and will expand over the coming weeks; Microsoft 365 Premium...

Why MFA Matters in Microsoft 365 (and How to Enforce It the Right Way)

If you’re still relying on passwords to protect Microsoft 365, you’re leaving a critical gap in your security. A single phished credential can be enough to access email, files, chats, and sensitive business data. Multi-Factor Authentication (MFA) adds a second verification step—such as an Authenticator prompt, a one-time code, or a security key—so a password alone can’t grant access. As a Microsoft 365 administrator and technology enthusiast, I’ve seen firsthand how implementing MFA dramatically reduces account takeover risk and strengthens organisational security. Who this is for This guide is aimed at Microsoft 365 administrators, IT teams, and business leaders looking for practical steps to bolster tenant security with consistent MFA enforcement. Whether you’re managing a small business or a larger enterprise, the principles here apply to everyone seeking to protect critical business data. The goal: require MFA for interactive sign-ins Enforcing MFA for all interactive use...

6 ways to check SharePoint Usage reports

Introduction SharePoint administrators always want to know which user is accessing any site or site collection extensively and which user has access to the site but not using it. The other site details required are related to traffic, access, edit, etc. The user's behavior changes as per the nature of the site and their working department like project team site, department site, and managerial workspace. Fortunately, Microsoft 365 has given multiple methods to generate or view various reports related to SharePoint usage. We will check all the methods one by one and see how they provide the required results. Several options are available at different parts in SharePoint Site or Admin Center where the Administrator can check status or create reports. Method-1. Check Site Usage in Site Contents Page. The Site contents page shows the analytics data like their unique visitors, total site visits, average time spent by the user, etc. The report can be generated for multiple days like 7, 3...