1

I have the below code to the the relative server URL. How do i get the absolute url so I can add it to an onclick object? Is there a get_serverRelativeUrl() equivalent function for absolute URL?

<script type="text/javascript">

var list;
var listRootFolder;
ExecuteOrDelayUntilScriptLoaded(init, "sp.js");

function init() {

    //load site
    var currentcontext = new SP.ClientContext('/StrategicProjectOffice');
    list = currentcontext.get_web().get_lists().getByTitle('Test List');
    listRootFolder = list.get_rootFolder();

    currentcontext.load(list, 'Title', 'Id');
    currentcontext.load(listRootFolder);
    currentcontext.executeQueryAsync(Function.createDelegate(this, result), Function.createDelegate(this, oncListQueryFailed));
}

function result() {
    var listID   = list.get_id();
    var listName = list.get_title();
    var listURL  = listRootFolder.get_serverRelativeUrl();
    console.log(listURL);
}

function oncListQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

2 Answers 2

3

For Document library:

Just append the _spPageContextInfo.siteAbsoluteUrl as shown below code to get absolute url of the list.

function result() {
    var listID   = list.get_id();
    var listName = list.get_title();
    var listURL  = listRootFolder.get_serverRelativeUrl();
    var absUrl = _spPageContextInfo.siteAbsoluteUrl +"/"+ listURL; //absUrl is now absolute url of the list.
}

For List:

Just append the _spPageContextInfo.webAbsoluteUrl as shown below code to get absolute url of the list. Just update methods as shown below.

function init() {

    //load site
    var currentcontext = new SP.ClientContext('/StrategicProjectOffice');
    list = currentcontext.get_web().get_lists().getByTitle('Test List');
    listRootFolder = list.get_rootFolder();
    web = currentcontext.get_web();

    currentcontext.load(list, 'Title', 'Id');
    currentcontext.load(web);
    currentcontext.load(listRootFolder);
    currentcontext.executeQueryAsync(Function.createDelegate(this, result), Function.createDelegate(this, oncListQueryFailed));
}

function result() {
    var listID   = list.get_id();
    var listName = list.get_title();
    var listURL  = listRootFolder.get_serverRelativeUrl();
    var absUrl = web.get_url() + "/Lists/" + listRootFolder.get_name(); //absUrl is now absolute url of the list.
}
1
  • This assumes that the list is in the same site that the script is running on. Wouldn't work if it was on a different site. Commented Mar 8, 2017 at 11:55
0

You can make use of the _spPageContextInfo to create a full url. I couldnt find a specific method, so i modified your code as below. This is will work for Lists only, for document library you need to remove /Lists/ from the code. Not necessarily the best approach, but works as expected.

var list;
var listRootFolder;
ExecuteOrDelayUntilScriptLoaded(init, "sp.js");

function init() {

    //load site
    var currentcontext = new SP.ClientContext('/StrategicProjectOffice');
    list = currentcontext.get_web().get_lists().getByTitle('Test List');
    listRootFolder = list.get_rootFolder();

    currentcontext.load(list, 'Title', 'Id');
    currentcontext.load(listRootFolder);
    currentcontext.executeQueryAsync(Function.createDelegate(this, result), Function.createDelegate(this, oncListQueryFailed));
}

function result() {
    var listID   = list.get_id();
    var listName = list.get_title();
    var listURL  = listRootFolder.get_serverRelativeUrl();
    console.log(listURL);

    var fullUrl = _spPageContextInfo.webAbsoluteUrl + "/Lists/" + listRootFolder.get_name();
    console.log(fullUrl);
}

function oncListQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

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.