Export (0) Print
Expand All

How to: Use the client chrome control in apps for SharePoint

apps for SharePoint

Learn how to use the chrome control in apps in SharePoint 2013.

Last modified: May 11, 2014

Applies to: apps for SharePoint | Office 365 | SharePoint Foundation 2013 | SharePoint Server 2013

The chrome control in SharePoint 2013 enables you to use the header styling of a specific SharePoint site in your app without needing to register a server library or use a specific technology or tool. To use this functionality, you must register a SharePoint JavaScript library through a standard <script> tag. You can provide a placeholder by using an HTML div element and further customize the control by using the available options. The control inherits its appearance from the specified SharePoint website.

To follow the steps in this example, you need the following:

  • Microsoft Visual Studio 2012

  • Office Developer Tools for Visual Studio 2012

  • A SharePoint 2013 development environment (app isolation required for on-premises scenarios)

For guidance on how to set up a development environment that fits your needs, see Start building apps for Office and SharePoint.

Core concepts to know before using the chrome control

The following table lists useful articles that can help you understand the concepts involved in a scenario that uses the chrome control.

Table 1. Core concepts for using the chrome control

Article title

Description

Overview of apps for SharePoint

Learn about the new app model in SharePoint 2013 that enables you to create apps, which are small, easy-to-use solutions for end users.

UX design for apps in SharePoint 2013

Learn about the user experience (UX) options and alternatives that you have when building apps for SharePoint.

Host webs, app webs, and SharePoint components in SharePoint 2013

Learn about the distinction between host webs and app webs. Find out which SharePoint 2013 components can be included in an app for SharePoint, which components are deployed to the host web, which components are deployed to the app web, and how the app web is deployed in an isolated domain.

A cloud-hosted app includes at least one remote component. For more information, see Choose patterns for developing and hosting your app for SharePoint. To use the chrome control in your cloud-hosted app, follow these steps:

  1. Create the app for SharePoint and remote web projects.

  2. Send default configuration options in the query string.

  3. Add a webpage to the web project.

Figure 1 shows a remote webpage with the chrome control.

Figure 1. Remote webpage with the chrome control

A remote web page with the chrome control

To create the app for SharePoint and remote web projects

  1. Open Visual Studio 2012 as administrator. (To do this, right-click the Visual Studio 2012 icon on the Start menu, and choose Run as administrator.)

  2. Create a new project using the App for SharePoint 2013 template.

    Figure 2 shows the location of the App for SharePoint 2013 template in Visual Studio 2012, under Templates, Visual C#, Office SharePoint, Apps.

    Figure 2. App for SharePoint 2013 Visual Studio template

    App for SharePoint 2013 Visual Studio template
  3. Provide the URL of the SharePoint website that you want to use for debugging.

  4. Select Provider-hosted as the hosting option for your app. For a SharePoint-hosted code sample, see Execute basic tasks using the JavaScript object model (JSOM).

    After the wizard finishes, you should have a structure in Solution Explorer that resembles Figure 3.

    Figure 3. App for SharePoint projects in Solution Explorer

    App for SharePoint projects in Solution Explorer

    Depending on your development environment, you might have to set the SSL Enabled property of the web project to false.

To send default configuration options in the query string

  1. Open the Appmanifest.xml file in the manifest editor.

  2. Add the {StandardTokens} token and an additional SPHostTitle parameter to the query string. Figure 4 shows the manifest editor with the configured query string parameters.

    Figure 4. Manifest editor with query string parameters for the chrome control

    Manifest editor with query string parameters

    The chrome control automatically takes the following values from the query string:

    • SPHostUrl

    • SPHostTitle

    • SPAppWebUrl

    • SPLanguage

    {StandardTokens} include SPHostUrl and SPAppWebUrl.

To add a page that uses the chrome control in the web project

  1. Right-click the web project, and add a new Web Form.

  2. Copy the following markup, and paste it in the ASPX page. The markup performs the following tasks:

    • Loads the AJAX library from the Microsoft CDN (Content Delivery Network).

    • Loads the jQuery library from the Microsoft CDN.

    • Loads the SP.UI.Controls.js file using the jQuery function getScript.

    • Defines a callback function for the onCssLoaded event.

    • Prepares the options for the chrome control.

    • Initializes the chrome control.

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Chrome control host page</title>
        <script 
            src="//ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" 
            type="text/javascript">
        </script>
        <script 
            type="text/javascript" 
            src="//ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js">
        </script>      
        <script 
            type="text/javascript"
            src="ChromeLoader.js">
        </script>
    <script type="text/javascript">
    "use strict";
    
    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)
    });
    
    // Callback for the onCssLoaded event defined
    //  in the options object of the chrome control
    function chromeLoaded() {
        // When the page has loaded the required
        //  resources for the chrome control,
        //  display the page body.
        $("body").show();
    }
    
    //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": "Chrome control app",
            "appHelpPageUrl": "Help.html?"
                + document.URL.split("?")[1],
            // The onCssLoaded event allows you to 
            //  specify a callback to execute when the
            //  chrome resources have been loaded.
            "onCssLoaded": "chromeLoaded()",
            "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];
        }
    }
    </script>
    </head>
    
    <!-- The body is initally hidden. 
         The onCssLoaded callback allows you to 
         display the content after the required
         resources for the chrome control have
         been loaded.  -->
    <body style="display: none">
    
        <!-- Chrome control placeholder -->
        <div id="chrome_ctrl_placeholder"></div>
    
        <!-- The chrome control also makes the SharePoint
              Website stylesheet available to your page -->
        <h1 class="ms-accentText">Main content</h1>
        <h2 class="ms-accentText">The chrome control</h2>
        <div id="MainContent">
            This is the page's main content. 
            You can use the links in the header to go to the help, 
            account or contact pages.
        </div>
    </body>
    </html>
    
  3. You can also use the chrome control in a declarative way. In the following code example, the HTML markup declares the control without using JavaScript code to configure and initialize the control. The following markup performs the following tasks:

    • Provides a placeholder for the SP.UI.Controls.js JavaScript file.

    • Dynamically loads the SP.UI.Controls.js file

    • Provides a placeholder for the chrome control and specifies the options inline with the HTML markup.

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Chrome control host page</title>
        <script 
            src="http://ajax.aspnetcdn.com/ajax/4.0/1/MicrosoftAjax.js" 
            type="text/javascript">
        </script>
        <script 
            type="text/javascript" 
            src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js">
        </script>      
        <script type="text/javascript">
        var hostweburl;
    
        // Load the SharePoint resources.
        $(document).ready(function () {
    
            // Get the URI decoded app web URL.
            hostweburl =
                decodeURIComponent(
                    getQueryStringParameter("SPHostUrl")
            );
    
            // The SharePoint js files URL are in the form:
            // web_url/_layouts/15/resource.js
            var scriptbase = hostweburl + "/_layouts/15/";
    
            // Load the js file and continue to the 
            // success handler.
            $.getScript(scriptbase + "SP.UI.Controls.js")
        });
    
        // 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];
            }
        }
        </script>
    </head>
    <body>
    
        <!-- Chrome control placeholder 
               Options are declared inline.  -->
        <div 
            id="chrome_ctrl_container"
            data-ms-control="SP.UI.Controls.Navigation"  
            data-ms-options=
                '{  
                    "appHelpPageUrl" : "Help.html",
                    "appIconUrl" : "siteIcon.png",
                    "appTitle" : "Chrome control app",
                    "settingsLinks" : [
                        {
                            "linkUrl" : "Account.html",
                            "displayName" : "Account settings"
                        },
                        {
                            "linkUrl" : "Contact.html",
                            "displayName" : "Contact us"
                        }
                    ]
                 }'>
        </div>
        
        <!-- The chrome control also makes the SharePoint
              Website style sheet available to your page. -->
        <h1 class="ms-accentText">Main content</h1>
        <h2 class="ms-accentText">The chrome control</h2>
        <div id="MainContent">
            This is the page's main content. 
            You can use the links in the header to go to the help, 
            account or contact pages.
        </div>
    </body>
    </html>
    

    The SP.UI.Controls.js library automatically renders the control if it finds the data-ms-control="SP.UI.Controls.Navigation" attribute in a div element.

To build and run the solution

  1. Make sure that the app for SharePoint project is set as the startup project.

  2. Press the F5 key.

    Note Note

    When you press F5, Visual Studio builds the solution, deploys the app, and opens the permissions page for the app.

  3. Choose the Trust It button.

  4. Click the ChromeControlCloudhosted app icon.

  5. When you use the chrome control in your webpages, you can also use the SharePoint website style sheet, as shown in Figure 4.

    Figure 4. SharePoint website style sheet used in the page

    SharePoint website stylesheet used in a page
Table 2. Troubleshooting the solution

Problem

Solution

Unhandled exception SP is undefined.

Make sure your browser loads SP.UI.Controls.js file.

The chrome control does not render properly.

The chrome control only supports document modes Internet Explorer 8 and superior. Make sure your browser renders your page in document mode Internet Explorer 8 or superior.

Show:
© 2014 Microsoft