Wednesday, August 6, 2008

Using relative Url ( $SPUrl )

You need to reference a file ( image , css or javascript file ) stored in one of the local Library using a relative Url

You can refer to the file like this "/LibraryName/filename.extension" . This works fine only when you are working with the site collection at root level. The browser doesn't know or care about site collections, and interprets the path as being relative to the website.

In this case, in a site collection at "/sites/NewSiteCollection" this path will still resolve to
and not

Use $SPUrl token as shown below

<% $SPUrl:~SiteCollection/LibraryName/filename.extension %>
<% $SPUrl:~Site/LibraryName/filename.extension %>
Another important thing to note is that you can only use this with controls having runat="server" , otherwise sharepoint will not be able to resolve the $SPUrl token.

Applies to:
MOSS only ( out of box ) ,
For WSS you need to write your own Expression Builder and register it in WebConfig