Use ctx.CurrentItem.FileRef in DispForm

Hi all,
I'd like to customize a document library display form to show the file if it's a jpg.

I can do it in the list view but not in the display form, the ctx.CurrentItem.FileRef and the ctx.CurrentItem.File_x0020_Type are Undefined.

(function() {
    var overrideCtx = {};
    overrideCtx.Templates = {};
    overrideCtx.Templates.Fields = {
        'Description': {
            'DisplayForm': customDisplayField,
            'View': overrideFieldTemplate
      }
    };

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);
})();

function customDisplayField(ctx) {
    return "<img src='" + fileRef + "' style='max-width:200px;'/>";
}
function overrideFieldTemplate(ctx) {

    if (ctx.CurrentItem.File_x0020_Type === "jpg") {
        return "<img src='" + ctx.CurrentItem.FileRef + "' style='max-width:200px;'/>";
    }
    else {
        return "<i>no preview available</i>";
    }
}

Is there a way to retrieve the file path and extension in the display form?
I know that can be a silly question but I'm really new to jsLink, I've started just yesterday.

thankyou
Cinzia



  • Edited by CinziaTa Wednesday, August 19, 2015 2:10 PM
August 19th, 2015 2:08pm

Can you try something like ctx.CurrentItem["FileRef"]?
Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 2:29pm

Check If you can use this for 

ctx.CurrentItem.File_x0020_Type
window.location.href.split('.')[window.location.href.split('.').length - 1]

August 19th, 2015 2:31pm

I did try, it's always undefined

function customDisplayField(ctx) {
    return "<img src='" + ctx.CurrentItem["FileRef"] + "' style='max-width:200px;'/>";
}

Free Windows Admin Tool Kit Click here and download it now
August 19th, 2015 2:41pm

solved using:

    var fileRef = ctx.CurrentItem.FileLeafRef.FileUrl;
    var fileLeafRef = ctx.CurrentItem.FileLeafRef.BaseName;

August 24th, 2015 10:04am

 var fileRef = ctx.CurrentItem.FileLeafRef.FileUrl;
    var fileLeafRef = ctx.CurrentItem.FileLeafRef.BaseName;

//another ways is

   var fileLeafRef = ctx.CurrentItem.FileLeafRef.EncodedAbsUrl;

fileLeafRef=fileLeafRef.split(".");

you will get the extention in fileLeafRef[1]

Free Windows Admin Tool Kit Click here and download it now
August 28th, 2015 12:23pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics