How to: Use a SharePoint website's style sheet in apps for SharePoint
Published: July 16, 2012
Learn how to use a SharePoint 2013 website’s style sheet in an app for SharePoint.
Applies to: apps for SharePoint | Office 365 | SharePoint Foundation 2013 | SharePoint Server 2013
You can reference the style sheet of a SharePoint website in your app for SharePoint and use it to style your webpages by using the style sheet in SharePoint 2013. In addition, if someone changes the SharePoint website’s style sheet or theme, you can adopt the new set of styles in your app without modifying the style sheet reference in your app.
If your webpages use the chrome control or the app master page, the styles are already available for you to use, and you don’t have to reference the style sheet manually by using the procedure in this article.
To follow the steps in this example, the following are required:
Microsoft Visual Studio 2012
SharePoint development tools in 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 SharePoint style sheet in an app
The following table lists useful articles that can help you understand the concepts involved in a scenario that uses the SharePoint style sheet.
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.
Learn about the user experience (UX) options and alternatives that you have when building apps for SharePoint.
Learn about the difference 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.
This code example uses an autohosted app to show you how to use the SharePoint website’s stylesheet. Autohosted apps are installed to a host web on the user's SharePoint Online tenancy, with Windows Azure components automatically installed into a Windows Azure Web Site account. For more information, see Hosting options for apps for SharePoint.
To use the style sheet in autohosted apps, follow these steps:
Create the app for SharePoint and remote web projects.
Force the app web provisioning by creating a blank page.
Add a webpage to the web project, and reference the style sheet.
Edit the StartPage element in the app manifest.
Figure 1 shows a SharePoint webpage that is using the style sheet.
To create the app for SharePoint and remote web projects
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.)
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.
Provide the SharePoint website’s URL that you want to use for debugging.
Choose Autohosted or Provider-hosted as the hosting option for your app.
After the wizard finishes, you should have a structure in Solution Explorer that resembles Figure 3.
To force the app web provisioning by creating a blank page
Right-click the app for SharePoint project, and add a new module.
Right-click the new module, and add a new item.
Under Visual C# items, Web, choose HTML Page. Rename the page to blank.html.
Delete the contents of blank.html.
To add a webpage that references the style sheet in the web project
Right-click the web project, and add a new Web Form. Rename the Web Form to StyleConsumer.aspx.
Replace the contents of the Web Form .aspx file with the following code. The code performs the following tasks:
Loads the blank.html page from the app web in an invisible IFrame.
Loads the defaultcss.ashx file from the app web.
Uses the available styles.
In some cases, the user has to be authenticated to SharePoint before your page will be able to download the CSS and images for styling. Link tags do not automatically authenticate a user who is not already signed in. Consider loading a page resource from the app web in your webpage to force the user’s authentication before linking to the CSS file. In this example, the blank.html page is loaded in an invisible IFrame.
To edit the StartPage element in the app manifest
Double-click the AppManifest.xml file in Solution Explorer.
On the Start page drop-down menu, choose the webpage that uses the style sheet.
To build and run the solution
Make sure that the app for SharePoint project is set as the startup project.
Press the F5 key.
When you press F5, Visual Studio builds the solution, deploys the app, and opens the permissions page for the app.
Choose the Trust It button.
Click the StylesheetBasic app icon.
Figure 4 shows the resulting webpage using the SharePoint styles.
You can also go to the host web and change the theme. Then reload the app webpage to use the new styles.
Visual Studio does not open the browser after you press the F5 key.
Set the app for SharePoint project as the startup project.
Set the SSL Enabled property of your web project to false. In the app for SharePoint project, set the Web Project property to None, and then set the property back to your web project’s name.
This article demonstrated how to use the style sheet in an app for SharePoint. As a next step, you can learn about other UX components that are available for apps for SharePoint. To learn more, see the following:
July 16, 2012