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%>" />
<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”
[…] a CSS file, but these aren’t used in the master page we’ve installed. Unfortunately, I’ve yet to figure out how to do relative URLs that are WSS friendly, and this solution should work with WSS as is (though I’ve only tested on a MOSS […]
According to the book Real World SharePoint 2007, CHapter 6, Page 155, this feature is only available through MOSS, and not WSS.
Yup, that’s correct – see the final paragraph. I tried a WSS install – the binary didn’t get installed.
What if it was a source for an image tag?