[SharePoint] Referencing CSS and JavaScript


Here's another one that has been widely covered, yet, not in depth.

When creating custom master pages, a link for css and javascript files is required.
What to do?

- Use AlternateCssUrl property from SPWeb object?
- Use a relative path, such as "/Style Library/theme.css"?
- What about using a site scoped URL?

So lots of blogs tell us to use SPUrl property , and it does work.
~site for the current web site
~sitecollection for the current site collection url (root web site)
 

<link runat="server" rel="stylesheet" type="text/css" href="<% $SPUrl:~SiteCollection/Lists/CustomThemeList/theme.css%>" />
<script type="text/javascript" src='<asp:Literal runat="server" Text="<%$SPUrl:~SiteCollection/Lists/CustomThemeList/js/script.js%>" />' ></script>


Unfortunatelly, SPUrl is not supported on SharePoint Foundation (WSS).

   
So let's try using ProjectProperty instead, which supports WSS as well as Server versions of SharePoint.
SiteUrl for the root site of the site collection
Url for the current web site


<asp:literal runat="server" Text="<link type='text/css' rel='stylesheet' href='"/>
    <SharePoint:ProjectProperty Property="SiteUrl" runat="server" />
    <asp:literal runat="server" Text="/CustomThemeList/theme.css'/>"/>

    <script type="text/javascript" src='<SharePoint:ProjectProperty Property="SiteUrl" runat="server" />/CustomThemeList/js/jquery-1.8.3.min.js' ></script>
    <script type="text/javascript" src='<SharePoint:ProjectProperty Property="SiteUrl" runat="server" />/CustomThemeList/theme.js' ></script>   


Comments

Popular posts from this blog

Breaking down document locking in SharePoint

Working around X-Frame-Options for iframes

Document ID not being generated