in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

benrobb's blog

How to create an img tag relative to the current Site Collection

I was creating a master page today which was going to be deployed to a farm with many site collections, and I needed to reference an image which was in the local Style Library directly. An easy task if your site collection is at the root of a web application:

< img src=" / Style%Library/Images/MyImage.jpg" alt="My Image"/>

This works fine until you use that master page on a site collection which isn't at the root. The browser doesn't know or care about site collections, and interprets the src path as being relative to the website. In this case, in a site collection at "/sites/NewSiteCollection" this path should have been:

< img src=" / sites/NewSiteCollection/Style%Library/Images/MyImage.jpg" alt="My Image"/> 

Interestingly, there are no <img> tags in any of the out of the box master pages or page layouts. The answer is to use the same $SPUrl syntax that the <SharePoint:CSSLink> tag in, for example, default.master. The syntax for this is:

<img src="<% $SPUrl:~SiteCollection/Style%Library/Images/MyImage.jpg %>" alt="My Image" runat="server"/>

[It is important to include runat=server, because otherwise the literal text (e.g. <% $SPUrl...) will be sent to the client without any processing by the server.]

Simple really. For future proofing I'm always going to use this syntax in my master pages and page layouts.

PS: Yes. So its been a while since I blogged. I'm still alive, just very busy at work. Rather than try and produce big in-depth blogs, its probably easier to do shorter ones like this.

Comments

 

Knowledge with Neil » Blog Archive » Relative URLs in Sharepoint Web Parts said:

Pingback from  Knowledge with Neil  &raquo; Blog Archive   &raquo; Relative URLs in Sharepoint Web Parts

April 21, 2008 4:07 AM
 

Jason said:

thanks for the info!

invalueable

May 21, 2008 9:07 AM
 

idsb(c) said:

When I try this $SPUrl: construction on a master page from a wss site it gives an error when trying to view a page:

Compiler Error Message: CS0234: The type or namespace name 'Publishing' does not exist in the namespace 'Microsoft.SharePoint' (are you missing an assembly reference?)

Is this only working for publishing sites? How to get it to work on a wss site?

June 4, 2008 9:30 AM
 

theBlindRef said:

idsb(c) ... I was looking for the same information today also. I added added the page declaration: <%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

to the top of the page and I no longer see the error.

June 4, 2008 1:58 PM
 

novolocus.com » Relative URLs in SharePoint Sites using $SPUrl said:

Pingback from  novolocus.com &raquo; Relative URLs in SharePoint Sites using $SPUrl

June 23, 2008 9:18 AM
 

Pedro Silva said:

Thanks guys :)

i have this:

<SharePoint:ApplicationPageLink ID="ApplicationPageLink" runat="server" Text="My Link" NavigateUrl="<% $SPUrl:~sitecollection/Pages/MyPage.aspx%>"></SharePoint:ApplicationPageLink>

inside a custom user control (.ascx)

all i was missing was:

<%@ Register Tagprefix="PublishingWebControls" Namespace="Microsoft.SharePoint.Publishing.WebControls" Assembly="Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

July 11, 2008 5:33 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts