SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have several links inside content editor webparts and on webpages that reference the site they are in. For example:

 <a href="/sites/ABC/page.aspx">Link</a>

Site ABC is being saved as a site template, and will be deployed several times.

How can I create a dynamic link that will update when the site template is deployed?

For example:

<a href="{dynamic site URL}/page.aspx">Link</a>

So if site DEF were deployed, it's URL would result, instead of a link back to ABC (the template).

Thanks in advance.

share|improve this question
up vote 4 down vote accepted

I haven't tested this, but I would think you can add it with a javascript maybe. You already have the _spPageContextInfo on your sites.

<script type="text/javascript">
document.write('<a href=\"'+_spPageContextInfo.webServerRelativeUrl+'/page.aspx\">link</a>');

That's just an example, probably not the best way to add js variables to a tag.

share|improve this answer
I used the variable, but noticed that editing the webpart triggers document.write(). To get around that I assigned a variable link1=_spPageContextInfo.webServerRelativeUrl+'/page.aspx' and since I was placing hte link around an image, wrote a basic function to be run onclick() to redirect the page. Thanks for the help!! This was bothering me BIG time. – mjrobichaud Apr 10 '12 at 19:23
Hehe, I guess it did, I didn't test it in a content editor myself. I just didn't come up with a better example on the fly for getting the variable in :) Glad it helped – Anders Aune Apr 11 '12 at 8:22

Tough one! I can't think of any OOTB way to do this as the relative url tokens only work in masterpages. Ways that will work would be to create your webparts as custom ones you can override the default behaviour of, or probably easier to create a custom site definition rather than a site template, and add a feature to update these links as the site is created... Other things I can think of would be to create a custom user control and add it to the masterpage. This could detect a flag stored in the propertybag of the web to see whether to run or not. It could then do your webpart updates. This might involve the least coding... Please post back and let me know how you do this as I'd be interested to find out!

share|improve this answer
check result above – mjrobichaud Apr 10 '12 at 19:25

There is a way, but you will have to do your work in master pages. The following tag is rendered in a sharepoint 2010 master page as ahref, and the relativity is passed on to sites created as templates.

<SharePoint:SPLinkButton runat="server" NavigateUrl="~site/SitePages/dashboard.aspx" id="v3idNavLinkTrackerDash"  Text="Now is the time for all good men." />
share|improve this answer

I use SPUrl and the base HTML tag, in the master page:

<base  href="<%$SPUrl:~Site/%>"  runat="server" />

and your links will be like

<a href="page.aspx"> Link</a>

The problem is it doesn't work with IE LOL

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.