SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

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.


Posted 04-20-2007 11:04 AM by BenRobb

Comments

Knowledge with Neil » Blog Archive » Relative URLs in Sharepoint Web Parts wrote Knowledge with Neil &raquo; Blog Archive &raquo; Relative URLs in Sharepoint Web Parts
on 04-21-2008 4:07 AM

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

Jason wrote re: How to create an img tag relative to the current Site Collection
on 05-21-2008 9:07 AM

thanks for the info!

invalueable

idsb(c) wrote re: How to create an img tag relative to the current Site Collection
on 06-04-2008 9:30 AM

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?

theBlindRef wrote re: How to create an img tag relative to the current Site Collection
on 06-04-2008 1:58 PM

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.

novolocus.com » Relative URLs in SharePoint Sites using $SPUrl wrote novolocus.com &raquo; Relative URLs in SharePoint Sites using $SPUrl
on 06-23-2008 9:18 AM

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

Pedro Silva wrote re: How to create an img tag relative to the current Site Collection
on 07-11-2008 5:33 AM

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" %>

Mint wrote re: How to create an img tag relative to the current Site Collection
on 06-19-2009 6:32 AM

I am using your code in a custom field control which is trying to display an image from an image collection. I am building the <img> tag dynamically and assigning it to a literal control which in turn displays the image. For your code to work for me is there anything else i need to do because in current form it does not work.

Highly appreciate any help.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.