Monday, October 20, 2014

Relative Path Using SPUrlExpressionBuilder & URL Tokens

Many time you need to provide relative path when you are working with image, js or css files. Of course you can use hard coded (Absolute) path but it fails after changing domain name or environment. Absolute path is more useful when linking to another website. You can also build relative path using ../ but it fails when you add reference in master page.

Fortunately, SharePoint provides SPUrlExpressionBuilder and URL tokens to generate the relative url. You can use following url tokens:
  • ~Site - Resolve to current web
  • ~SiteCollection - Resolve to current site collection root
URL Tokens can be use only with server side control. Followings are snippets to generate the relative url:

JavaScript



CSS



Favicon



Image



Hyperlink



Server side


No comments:

Post a Comment