0

I've applied a csr js file to overwrite the rendering of dispform.aspx for a simple list. This displays every field correctly except for a publishing image and the created by fields which return "undefined" indicating they are not available as ctx objects. When applied to a list view rather than a list form, the 2 fields do display.

(function () {
/*
 * Initialize the variable that store the overrides objects.
 */
var overrideCtx = {};
overrideCtx.Templates = {}; 
//  Assign functions or plain html strings to the templateset objects:
//  header, footer and item.
//  This template is assigned to the CustomItem function.
overrideCtx.Templates.Item = CustomItem;


//       Register the template overrides.

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrideCtx);

})();


function CustomItem(ctx) {
// Build a listitem entry for every item in the list.

var ret = "<div class='news ms-blog-postBody'><div class='fright col-md-3 
col-sm-3 panel panel-white'>" + ctx.CurrentItem.Publishing_x0020_Image + " 
<div class='center'><span class='ms-rteStyle-Emphasis ms-textSmall'>" + 
ctx.CurrentItem.Author[0].title + "<br />" + 
ctx.CurrentItem.Job_x0020_Title + "<br />" + 
ctx.CurrentItem.Business_x0020_Unit_x0020__x002f + " </span></div></div>-- 
more logic here--- " 
</div>";
    return ret;
}

1 Answer 1

0

You can get the author of the current user from ctx.ListData in the display form.

ctx.ListData.Items[0]["Author"]

To retrieve the value of a publishing image field, you may need to use client object model.

var listname='<list name>';
var itemId =<current item id>; 
var clientContext ;
var oItem;

ExecuteOrDelayUntilScriptLoaded(getPublishingImage, "sp.js");

function getPublishingImage() {

    var clientContext = new SP.ClientContext();
    var oList = clientContext.get_web().get_lists().getByTitle(listname);
    oItem = oList.getItemById(itemId);

    clientContext.load(oItem);

    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceededRetrieve), 
        Function.createDelegate(this, this.onQueryFailed)
    ); 
}

function onQuerySucceededRetrieve(sender, args) {
    // get item information
    var image = oItem.get_item('Publishing_x0020_Image');
    var user = oItem.get_item('Author');    
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

SharePoint Online: Retrieve list items from SharePoint list using client object model

Update: Some screenshots of ctx object in display form

enter image description here enter image description here

2
  • Thanks but the suggestion for the created by field throws an error in this context. How would I implement the publishing image idea in the code as it is above?
    – matt
    Jul 13, 2018 at 0:52
  • What is the error? The ctx.CurrentItem only works for the list view. It is null in the display form. I update my answer and add some screenshots of outputs from display form. You can use the code to get the field value and then format the rendering using jslink with csr. Jul 13, 2018 at 2:53

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.