In a document library's /Forms/AllItems.aspx, I am trying to render a column from ID to a file URL (FileLeafRef).
However this line of code:
clientContext = new SP.ClientContext.get_current();
is giving the error:
Uncaught TypeError: Cannot read property '
get_current' of undefined
I followed this reference in stackexchange to amend my code. The error no longer show up by rending is not triggered. In below code, CSR_Setup is triggered. However overrideNameFieldTemplate and getFileLeafRefbyID is not triggered at all. The Allitems.aspx shows up without error and no change.
What's wrong?
(function () {
ExecuteOrDelayUntilScriptLoaded(CSR_Setup, "SP.js");
function CSR_Setup(){
var overrideNameField = {};
overrideNameField.Templates = {};
overrideNameField.Templates.Fields = {
"InstructionLibID": { "View": overrideNameFieldTemplate }
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideNameField);
}
function overrideNameFieldTemplate(ctx) {
var InstructionLibID = ctx.CurrentItem.InstructionLibID;
var fileRef = getFileLeafRefbyID("Instruction",InstructionLibID);
if (InstructionLibID) {
return "<a href='"+ fileRef + "'>Download</a>";
}
else {
return "N/A";
}
}
function getFileLeafRefbyID(LibName,Id){
clientContext = new SP.ClientContext.get_current();
oWebsite = clientContext.get_web();
oList = oWebsite.get_lists().getByTitle(LibName);
this.oListItem = oList.getItemById(Id);
clientContext.load(this.oListItem);
clientContext.executeQueryAsync(
Function.createDelegate(this, successHandler),
Function.createDelegate(this, errorHandler)
);
function successHandler() {
return(oListItem.get_item('FileLeafRef'));
}
function errorHandler() {
console.log('Fail');
}
}
})();
InstructionLibIDis the internal name of your lookup column? – Submits May 27 '16 at 10:33getFileLeafRefByIDand also you do not have to donewonSP.ClientContext.get_current();. But none of those things should throw "Uncaught TypeError: Cannot read property 'get_current' of undefined" – Robert Lindgren May 27 '16 at 10:53ExecuteOrDelayUntilScriptLoaded(CSR_Setup, "SP.js");I will get the Uncaught type error. If I callExecuteOrDelayUntilScriptLoaded(CSR_Setup, "SP.js")it will call CSR_Setup without render anything and no error. – Mark L May 27 '16 at 11:00