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.

4 Answers 4


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.

  • 2
    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. Apr 10, 2012 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 Apr 11, 2012 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!

  • check result above Apr 10, 2012 at 19:25

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

  • This works if you add the code to the .master. Is there a way to add it to the .html?
    – Rothrock
    Jun 28, 2018 at 17:31

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." />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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