Question

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.

Était-ce utile?

La solution

I would double check that "Location" is truly the internal name of the field. Sometimes the display name can change, and to register the CSR override you need to use the internal name.

The easiest way to check that is to go to the List Settings page for the list, scroll down to the Columns section and click on the name of the column. on the Edit Column page, look at the URL for a query param "Field=", and there you will see the actual internal name of the field.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top