display List/library url in display template
-
17-01-2021 - |
質問
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
}
解決
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>
所属していません sharepoint.stackexchange