3

I am trying to add the url of libraries in the display template. ListUrl does not return any value.

So I thought using javascript to replace the filename to "". However it is throwing an error:

Object doesn't support property or method 'lastIndexOf' Any idea how to get the List Url in display templates?

var listPath = ctx.CurrentItem.Path; //Get Path
if (listPath.indexOf("Lists/") >= 0) {  //If contains List/
    var afterLists = listPath.substring(listPath.lastIndexOf("Lists/")+6); //get url after "Lists/"
    var listTitle = afterLists.split('/'); // split 
    listTitle[0] = listTitle[0].replace("_", " "); //get list title and replace _ with " "
    listTitle = listTitle[0]; // set list title in var
}
else  { //no Lists/ in path
    listPath = ""; //set empty
    listTitle = listPath;
} else if (listPath.indexOf("ces/") >= 0 && listPath.indexOf("Lists/") < 0) {  //If contains ces/ without lists
    var afterLists = listPath.substring(listPath.lastIndexOf("ces/")+4); //get url after "ces/"
    var listTitle = afterLists.split('/'); // split 
    listTitle[0] = listTitle[0].replace("_", " "); //get list title and replace "_" with " "
    listTitle = listTitle[0]; // set list title in var
}

1 Answer 1

3

You can use the OOTB managed property ParentLink which will have the URL of the list or document library.

In the <mso:ManagedPropertyMapping section of display template, add the ParentLink managed property.

After that, in your code you can use it as :

ctx.CurrentItem.ParentLink

or in your HTML as something like:

<a href="_#= ctx.CurrentItem.ParentLink =#_" title="All items">All items</a>
2
  • Oh man, I am not awake yet. I was going through the properties of ctx.CurrentItem and even I saw the returned url of "ParentLink", but my mind was stuck on javascript. Thanks man. Commented Jun 4, 2018 at 9:36
  • unfortunately, this does not seem to account for items stored inside folders. will just return the immediate parent folder, which being the root folder is fine, but will not always work Commented Feb 20, 2019 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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