0

In order to render certain controls in a view via JSLink, I need to check several conditions based on some fields we would like to keep hidden (not just uneditable in a grid view). How can I do that if fields seem to only be added to the ctx.CurrentItem object when they appear in the targeted view?

I would hate to have to make some synchronous ajax call on each list item or even one big synchronous call in order to build an array which I would use to search for each item because both methods seem extremely inefficient.

1

You stuff the Item data you need in a Calculated Column and use that Column(value) to proces the Item in your CSR code

But.. you can't get complex fields like Lookup, Multiple Choice into a Calculated Column.

=[Title] & "," & [Priority] & "," & [Status]

Then in your CSR code:

var values = CTXITEMVALUE.split(',');
2
  • So all calculated fields are included in the context even if we don't render them in the view? Or are you saying that you would simply use this calculated field for displaying the buttons or other html? I like this approach, but I'll need to come up with a better delimiter than a comma. – Dinerdo Jun 14 '16 at 18:34
  • No, they are not in the context. You abuse the CC output to get data into your CSR code – Danny '365CSI' Engelman Jun 14 '16 at 19:50
1

Another option is to include them in the view to get the data on the ctx.CurrentItem object, but then use CSR to ultimately hide them.

Even if you use Danny Engelman's workaround, you will still need to include that column in the view.

You are correct in noting that in order to get the data, you will either need to include the columns in the view, or make another call to the list. Only columns included in the view will show up on the ctx.CurrentItem object.


How to pull it off? There might be several ways, CSS could be one of them, you might be able to find the th header elements that correspond to those columns and add display: none to them, and then override the fields and return an empty string (i.e. no HTML) instead of the regular value.

But, what I would try first is this:

ctx.ListSchema.Field (notice singular, not plural) is an array of FieldSchema objects of the fields that are in the view. I know that you can add a fake column to the table by inserting a fake FieldSchema object into that array in OnPreRender. What I would try is, in OnPreRender, loop through that array, find the FieldSchema objects that correspond to the columns you want to hide, and splice them out of the array. Potentially you could trick SharePoint into not even thinking of rendering those fields, because they would no longer exist in the ListSchema.

Granted, I haven't tried that, so I don't know if it would really work, but that's what I'd start with.

2
  • I thought about this, but what would be the best way to hide those specific columns? To use a PostRender event, css, etc.? – Dinerdo Jun 14 '16 at 18:35
  • @Dinerdo updated my answer. – Dylan Cristy Jun 14 '16 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.