1

I need to rename the list headers and used the code as provided here:
Can I change the display name of column in a listview using JSlink
It works fine, but only for the current view. When I change the view, the code does not work anymore.

Is there any way to apply JSLink code for each view in the list?

| improve this question | | | | |
1

Yes, CSR overrides defined globally in a UserCustomAction script will be applied to all Views.

But, there is a bug, if CSR overrides are declared twice (one in a UserCustomAction script, the other as JSLink on a WebPart, then neither will work) , so be careful where you link your file.

CSR and JSLink are related but totally different technologies!!

CSR

(which most people incorreclty call JSLink) is the JavaScript definition that declares which UI elements need to be overriden with custom code (Client Side Rendering)

JSLink

Is the technique where a WebPart (or View) loads a JavaScript file.

This can be any file, does not have to be a CSR file On Office365 you can even link to any URI.. so if you are still smitten with jQuery you could load it from a CDN

The (Chrome Browser Extension) Cisar makes Editing CSR files a breeze. It does all the JSLink handling for you (for /style library/ linked files only)

UserCustomActions

CSR and JSLink are new in SP2013

ScriptLinks(and Blocks) set with UserCustomActions have been around since SP2010

They execute a JavaScript file (or inline scriptBlock) for every page in your SiteCollection (Site UAC) or just one Subweb (Web UAC)

So you can declare CSR overrides in a UAC script, without the need for JSLinks

If you do not want to mess with Powershell code...

Disclaimer: Neither of these technologies work in the SharePoint "New Experiences" (at time of writing: Document Library, Site Contents on Office365)
Microsoft has not yet allowed any branding or custom scripts on these pages, and says: 'if you need them switch back to Classic Mode'

See: https://support.office.com/en-us/article/Differences-between-the-new-document-library-experience-and-classic-mode-30e1aab0-a5cc-4363-b7f2-09e2ae07d4dc?ui=en-US&rs=en-US&ad=US

| improve this answer | | | | |
0

First place the JSLink code inside a JS file and place it in a library.

Now open each view page and edit web part properties and set the JSLink URL to the JS file.

| improve this answer | | | | |
0

In addition to what Danny has answered, a couple other things to note:

If you link to your CSR script using the JSLink property of a List View Web Part, then yes, you CSR script is actually attached to only the view that was active when you linked the script.

To have it be applied to every view, there are a couple things you can do.

  1. Manually link it to every view by switching to each view you use, editing the web part, and setting the JSLink property. This is tedious, and if anyone creates a new view, your CSR script will not be automatically linked.

  2. Link to the script by some other means. Danny has mentioned User Custom Actions, however, fields and content types also have JSLink properties you can use to link to CSR scripts. If your list uses content types, or if there is a particular field that is present in every view, you can use Powershell to set the JSLink property of the content type or field to link to the script. If done that way, the script will automatically apply to any view.

Your question's title though, the way you have worded it:

JSLink - apply different code depending on the current view

sounds like you want to have different CSR scripts run depending on the view. If this is the case, then linking to the scripts through the List View Web Part is exactly what you want to do, because doing it that way will link the script directly to the views themselves.

| improve this answer | | | | |

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.