SharePoint Conditional Formatting
In this post, I will show you, the non-developers how to use JS Link, and some extreme basics about how it works. Once you are armed with this knowledge and you want to research it further, you will better be able to translate what the developers are writing and maybe be able to use it.
Here we go, and remember that I am not a programmer and I don’t know anything technical, I’m just figuring it out as I go. This blog post was my inspiration.
Okay, I’m going to use a super simple example here, because mostly what I’m demonstrating is not the script language and how to program, but how to get JSLink actually working in your environment. Again, this is really cool to me, because it allows us to do conditional formatting in views and forms without the need for SharePoint Designer or InfoPath! The example is a SharePoint list of patients in a hospital. There is a column called “Progress Level”, and their level is either “Good” or “Bad”. Very simple. I want to show the good ones with green text and the bad ones in red. Actually, in order to demonstrate an image in conditional formatting versus a text color, the good ones will show in green text, and the bad ones will show a red icon, like this:
So here are the steps to do this:
- Create a custom list with a choice column in it called Progress. Make the two choice options Good and Bad. When you create this column, make sure you call it Progress, and you’re not just renaming some other column and calling it that. This is important to making it work correctly.
- Insert this new list on your homepage or some page on your SharePoint site, as a web part, and make sure the Progress column is showing.
- Download this file to your computer and extract the zip. It’s just a text file, but you’ll need to save it as a .JS file. See, I removed the .TXT and changed the Save as type to All Files.
- If you have access to the root level of your site collection, go there and go to Site Settings. In the Web Designer Galleries section, click Master Pages. Then click the Display Templates folder.
- Here is what you are prompted with, and what to fill in, and click SAVE.
- Here are what some of the settings mean:
Target Control Type:
View – a view in a list of multiple items
Form – a form that users are filling out, such as dispform/editform/newform.
Field – just a single field, although I can’t find any references that really explain what this one is for and when it would be used.Standalone: Every reference I’ve seen says to set this to Override, but no one says what the purpose of the other option (Standalone) would be.
Target Scope: This is the URL relative to the site collection. So here I put a / because I want this JS file to be available to use anywhere in the site collection. If I only wanted to be able to use this code on a sub-site called “IT”, I would type /IT in this box.
Target List Template ID: The list in my example is a custom list (see step 1), and the code for a custom list is 100. Here is a list of a bunch of other codes for the other list types. Scroll down to the section called Type, to see the long list of codes. I know it says 2010, but I can’t seem to find a list of updated codes for the latest version. There are only one or two new list types in 2013, and the rest are going to be the same.
If you’ve got your file uploaded now, you can skip to step 12.
- Go to step 6.
- Anyway, now that your JS file has been uploaded, you’ll need the URL to it. In the Files tab of the ribbon, click the View Properties button for this JS file you just uploaded. Right click on the name of the file and click Copy Shortcut.
- Now that you have the URL to the JS file, remember that web part from step two? Go to that web part in the browser, and open the web part properties toolpane. Expand the Miscellaneous section (more on that in my other post). Paste your URL in the JS Link box. Notice that when you save the web part, nothing magical happens. Go back into the web part properties and notice that the URL is now showing as relative, like since my library is “Test”, it changed the URL to /Test/PatientList.js
You need to put the ~site at the beginning, as shown here. If your JS file were being stored in a library at the root of your site collection, and you were referencing it from web parts in sub-sites, you would use ~sitecollection instead.
Now, save your page, and your web part looks like that that first screenshot (at least the progress column does). At step two, I instructed you to put your list on a page as a web part, but if you want to you can just go to any regular view of your list, click the (Settings) gear at the top right and click Edit Page. From there you can do step 13.
9/29 update: I see some confusion out there around this last step of using the link to the JS file. Please refer to a seperate blog post I wrote called SharePoint URL Basics. This will help you understand how to obtain the link to your site. In my example screenshot above, “Test” is a library in my site, and PatientList.js is a file in that library.
Wes Preston has a great blog, with a lot of posts about JS Link, and here’s a great “primer” that he wrote about it. There are also hundreds of other blog posts out there about JS Link.
I’ll get into dissecting the syntax in another post. In the meantime, feel free to dig in and see how the conditional formatting works, and browse the plethora of posts about it.