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>
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.
}
| improve this answer | |
  • 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. – wjervis Mar 8 '17 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());
}
| improve this answer | |

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.