0

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

1 Answer 1

4

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 and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.