I have a display template that uses a javascript file which I would like to have located at the root of my site (web application root).

Using $includeScript how can i refer to the file at this location. I would like to avoid hardcoding the full url if possible. I know ~siteCollection will look for it relative to the current site collection but I need to look for it relative to the entire website.

Thank you


It can be achieve different ways.

  • Quick way is combination of default JavaScript object and SharePoint page variable.

    var url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;

  • SharePint client Context - 1
    function GetSiteUrl() { var ctx = new SP.ClientContext(); var site = ctx.get_site(); ctx.load(site); ctx.executeQueryAsync(function(s, a){ alert(site.get_url() )}); }

  • SharePoint client Context - 2
    function initialize() { var clientContext = new SP.ClientContext.get_current(); siteCollec = clientContext.get_site(); clientContext.load(siteCollec); clientContext.executeQueryAsync(Function.createDelegate(this, getUrl), Function.createDelegate(this, getFailed)); } function getUrl() { alert(site.get_url()); } function getFailed() { alert('Failed to retrieve the server relative URL.'); }

  • Page Variable
    var siteCollectionURL = _spPageContextInfo.siteAbsoluteUrl;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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