Use a SharePoint website's style sheet in SharePoint Add-ins
Learn how to use a SharePoint website's style sheet in a SharePoint Add-in.
Note The name "apps for SharePoint" is changing to "SharePoint Add-ins". During the transition, the documentation and the UI of some SharePoint products and Visual Studio tools might still use the term "apps for SharePoint". For details, see New name for apps for Office and SharePoint.
You can reference the style sheet of a SharePoint website in your SharePoint Add-in and use it to style your webpages by using the style sheet in SharePoint. In addition, if someone changes the SharePoint website's style sheet or theme, you can adopt the new set of styles in your add-in without modifying the style sheet reference in your add-in.
Important If your webpages use the chrome control or the add-in 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.
Prerequisites for using the examples in this article
You need a development environment as explained in Get started creating provider-hosted SharePoint Add-ins.
Core concepts to know before using the SharePoint style sheet in a SharePoint Add-in
The following table lists useful articles that can help you understand the concepts involved in a scenario that uses the SharePoint style sheet.
Table 1. Core concepts before using the style sheet
|SharePoint Add-ins||Learn about the new add-in model in SharePoint that enables you to create add-ins, which are small, easy-to-use solutions for end users.|
|UX design for SharePoint Add-ins||Learn about the user experience (UX) options and alternatives that you have when building SharePoint Add-ins.|
|Host webs, add-in webs, and SharePoint components in SharePoint||Learn about the difference between host webs and add-in webs. Find out which SharePoint components can be included in a SharePoint Add-in, which components are deployed to the host web, which components are deployed to the add-in web, and how the add-in web is deployed in an isolated domain.|
Code example: Use a SharePoint website's style sheet in a SharePoint Add-in
This code example shows you how to use the SharePoint website's stylesheet. This enables your remote web application pages to match the look-and-feel of the pages in the SharePoint host web.
To use the style sheet in a SharePoint Add-in, follow these steps:
Create the provider-hosted SharePoint Add-in..
Force the add-in web provisioning by creating a blank page.
Add a webpage to the web project, and reference the style sheet.
Edit the element in the add-in manifest.
Figure 1 shows a SharePoint webpage that is using the style sheet.
Figure 1. Webpage using the style sheet
To create the SharePoint Add-in and remote web projects
Open Visual Studio as administrator. (To do this, right-click the Visual Studio icon on the Start menu, and choose Run as administrator.)
Create the provider-hosted SharePoint Add-in as explained in Get started creating provider-hosted SharePoint Add-ins and name itStylesheetAdd-in.
To force the add-in web provisioning by creating a blank page
Right-click the SharePoint Add-in 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 add-in web in an invisible IFrame.
Loads the defaultcss.ashx file from the add-in 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 add-in 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 add-in 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 SharePoint Add-in project is set as the startup project.
Press the F5 key.
Note When you press F5, Visual Studio builds the solution, deploys the add-in, and opens the permissions page for the add-in.
Choose the Trust It button.
Click the StylesheetBasic add-in icon.
Figure 2 shows the resulting webpage using the SharePoint styles.
Figure 2. Style sheet used in the page
- You can also go to the host web and change the theme. Then reload the add-in webpage to use the new styles.
Table 2. Troubleshooting the solution
|Visual Studio does not open the browser after you press the F5 key.||Set the SharePoint Add-in project as the startup project.|
|Certificate error.||Set the SSL Enabled property of your web project to false. In the SharePoint Add-in project, set the Web Project property toNone, and then set the property back to your web project's name.|
This article demonstrated how to use the style sheet in a SharePoint Add-in. As a next step, you can learn about other UX components that are available for SharePoint Add-ins. To learn more, see the following: