Relative URLs in SharePoint Sites using $SPUrl

So, I was really stuck with a Master Page that I’ve been building as a demo. This master page was for use on a site without the publishing features, such as a Team Site.

I wanted to provide a separate CSS file and an image for with this example. However, I hit a snag – how to get relative URLs to the CSS file and image that I was putting into the site?

For Publishing sites, this isn’t normally a problem – you put things such as the master page or images or CSS near the root, and then reference them from the root of the site collection. But from the subsite I was using, this proved hard.

Well, in the end I turned up this post from Ben Robb about using $SPUrl. It turns out that this works nicely for images, and for links to other content, such as CSS files:
<link id="css1" runat="server" rel="stylesheet" type="text/css" href="<% $SPUrl:~site/_catalogs/styles/mystyles.css%>" />
or
<img id="img1" src="<% $SPUrl:~site/images/badger.png%>" />

Unfortunately, it relies on the Microsoft.SharePoint.Publishing namespace, which means that this solution isn’t WSS friendly; certainly I couldn’t get it working. Or rather, maybe it isn’t – perhaps the Microsoft.SharePoint.Publishing namespace is available in WSS3, even if the publishing features themselves aren’t. Does anybody know about that?

4 thoughts on “Relative URLs in SharePoint Sites using $SPUrl

  1. Pingback: novolocus.com » Setting the Master Page of a Team Site with a Feature

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>