I am trying to get a super simple JS script to work on my SharePoint. I found this example here. This is what I am working with:
(function () {
var statusFieldCtx = {};
statusFieldCtx.Templates = {};
statusFieldCtx.Templates.Fields = {
"Location": { //The internal name of that column.
"View": StatusFieldViewTemplate
}
};
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(statusFieldCtx);
function StatusFieldViewTemplate(ctx) { //Function to change the status styling
var statusValue = ctx.CurrentItem[ctx.CurrentFieldSchema.Name]; //To get the current item's status value.
if (statusValue == "Equipment Room") {
return "<div style='background-color:green;color:white'>" + statusValue + "</div>";
}
if (statusValue == "Treatment Room") {
return "<div style='background-color:red;color:white'>" + statusValue + "</div>";
}
else{
return statusValue;
}
}
})();
This script does nothing at all. I did put in a alert("Override call worked");
to ensure that it is linked appropriately.
Am I missing something? This is working with SharePoint 2013.
alert
in theStatusFieldViewTemplate
function? Did you put a breakpoint after you assignstatusValue
so you can inspect it's value (and also maybe inspect thectx
object to see if you can see anything in there? What type of field is theLocation
field? – Dylan Cristy Apr 2 '19 at 19:45Location
field is aSingle line of text
type. Hmm, nope, theAlert
only worked in the main function, not theStatusFieldViewTemplate
function. . . How can I set a breakpoint? Sorry, this is rather new to me. – Shinobii Apr 2 '19 at 19:53Location
is truly the internal name of the field? Easiest way to check that is to go to the List Settings page for the list, scroll down to the Columns section and click the column name. On the "Edit Column" page, look at the URL for a query parameter "Field=" and there will be the internal name of the field. To set a breakpoint. open the browser dev tools (I think F12 on most). in Chrome there is a tab called "Sources" and in FF and IE I think it's called "Debugger" where you can find a list of the script... – Dylan Cristy Apr 2 '19 at 20:46Location
is actuallyx5vw
. Thanks for that! Still cannot get theStatusFieldViewTemplate
function to work. I will try the debugger and see what I can find. – Shinobii Apr 2 '19 at 20:58