Question


I am developing a SharePoint 2013 hosted app.
I would like to use in my app the color of the style site.
How can I retrieve the information of the style used by the site using JS?
Thanks,Nk

Was it helpful?

Solution

To use the host web's colours you'll need to dynamically load the host web's style sheet with some JavaScript. If you're using jQuery on the page, then you can use following block of JavaScript to inject the host web's style sheet onto your page.

(function () {
    // Retrieve the host web's URL from the query string
    var scriptbase = $.queryString('SPHostUrl') + '/_layouts/15/';

    // Create a <link> tag for the style sheet
    var $doclink = $('link').attr('rel', 'stylesheet');
    // The style sheet is loaded through an ASP.NET HTTP handler (defaultcss.ashx)
    $doclink.attr('href', scriptbase + 'defaultcss.ashx');

    // Add the style sheet link to the 
    $('head').append($doclink);
})();

If that doesn't work, you can also check out Microsoft's article How to: Use a SharePoint website's style sheet in apps for SharePoint for an example that doesn't require jQuery.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top