I'm wondering if is possible use the Id of the Lookup Field for obtain a custom column created in the list.


I have two lists called Books and Authors.

Here, Authors has a LookUp field to Books and Books has a Column called NumberOfPages (which is NOT part of the View that I’m using CSR).

The View has all columns of the Authors List and only this two columns of the Books (Id and Title) the default ones for a Lookup.

What I tried:

  • Update the List Settings of the Authors List for edit the NumberOfPages Column and in the section called Add a column to show each of these additional fields I checked the NumberOfPages field.

However, is NOT a option have visible this column NumberOfPages, but I need the info that this column contains, there’s a way to accomplish it?

A possible solution is add the NumberOfPages column in the View and after in the OnPostRender method dissapear this column from the View.

The approach what I'm trying to get explained in a cleaner way is detailed in this answer and with some adjustments with the code provided here I'm trying:

  1. Get the value of the NumberOfPages column.
  2. Save the obtained value in a var variable.
  3. After make some calculations which aren't part of my question, remove the column NumberOfPages from the View.

The following code this that I get at the moment editing the code provided in the links metioned before for try hide the NumberOfPages column, but the column stills visible in the View:

<script type="text/javascript">
(function () {

var coloringFieldContext = {};
coloringFieldContext.Templates = {};

coloringFieldContext.OnPostRender = hideFields;

//coloringFieldContext.Templates.Fields = {
//     "FieldName": { "View": coloringFieldTemplate }

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(coloringFieldContext    );

//function coloringFieldTemplate(ctx) {

//    var currentFieldValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name];
//    var currentRefferedFieldValue = ctx.CurrentItem["NumberOfPages"];

//   if (currentRefferedFieldValue === "Test") {
//       return "<span style='background: red;'>" + currentFieldValue + "    </span>";
// else {
//   return "<span style='background: green;'>" + currentFieldValue + "    </span>";
 // }

function hideFields(ctx) 
    var cell = $("div [name='NumberOfPages']").closest('th'); 
    var cellIndex = cell[0].cellIndex + 1; 

    $('td:nth-child(' + cellIndex + ')').hide(); 
    $('th:nth-child(' + cellIndex + ')').hide(); 

Code added inside Web-Page which has a Script Editor component instead in a script file separately and a View with the columns described at the beginning of the question.

I'm still reading and trying to understand this "new logic" of rendering info in SharePoint and I read some blogs for get informed, but there's not info about what I'm trying to get.


Your Answer

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