0

I need help, I'm very new to the SharePoint world.

I have a custom list for status indication with columns name as "Tool", "2017 q1" and "2017 q1-State". The "2017 q1-State" column has 2 options, "Ongoing" and "Terminating". How the list works is, based on the "2017 q1-State" value, the color of the content in "2017 q1" will change.

My Code (Reference link: Status Indicators on SharePoint Online):

(function () {

    var StatusFieldContext = {}; 
    StatusFieldContext.Templates = {};
    StatusFieldContext.Templates.Fields = {
        "2017 q1": { "View": Q1ValueFiledTemplate}
    };

SPClientTemplates.TemplateManager.RegisterTemplateOverrides(StatusFieldContext);
})();

// This function reflects the background color and format based on respective status field value
function Q1ValueFiledTemplate(ctx) {

    var q1x = ctx.CurrentItem._x0032_017_x0020_q1_x0020__x002d;  //Assign Counter variable to q1 status value
    var q1 = ctx.CurrentItem._x0032_017_x0020_q1; 

    // Return html element with appropriate color based on status value
    switch (q1x) {
        case "Ongoing":
            return "<span style='background-color:#65FE18;display:block;width:200px;height:35px;'>" + q1 + "</span>";           
            break;
        case "Terminating":
            return "<font style='background-color:#F7FF02;display:block;width:200px;height:35px;'>"+ q1 +"</font>";
            break;     
    }
}

Now, this code works fine for the list which has the same display and internal names, but it is not working for the lists which have different displays and internal names.

For your reference:

  • Column 1: Display Name is Tool and Internal name is Tool
  • Column 2: Display is "2017 q1" and Internal name is "_x0032_017_x0020_q1"
  • Column 3: Display is "2017 q1-State" and Internal name is "_x0032_017_x0020_q1_x0020__x002d"

Can anyone please tell me what is going wrong in my code?

2 Answers 2

0

I think you have already found the source of your problem - internal and display names are different in SharePoint.

The problem I see in your code is that in the part where you register the template overrides, I am pretty sure you should be using the internal name, not the display name.

So you need to change to this:

StatusFieldContext.Templates.Fields = {
    "_x0032_017_x0020_q1": { "View": Q1ValueFiledTemplate}
};
5
  • I did try this, still the same. Nothing getting displayed in the column. Commented Jan 18, 2018 at 20:43
  • Hi, I have it working now, the real problem was, I was trying to not display the"2017 q1-State" column. For me to have the coloring formating working in the column "2017 q1", I need to display the "2017 q1-State" too. Is there any way I can hide it using Javascript or Jquery ? Commented Feb 5, 2018 at 14:24
  • @ParinitaHirehal you should ask that as a new question! :) Commented Feb 5, 2018 at 19:24
  • I just did ...@Dylan Commented Feb 5, 2018 at 20:28
0

I just tried your code on a test list, and it worked - I got a green column for Ongoing and yellow for Terminating. Dylan is correct, you have to use the internal names throughout your code.

Are you sure it's actually running the code? Remember that when you reference your code on the webpart in the JSLink field, it has to start with ~site/ , like ~site/SiteAssets/Scripts/nameofcode.js - however you named things. Try sticking some 'alerts' , one after your (function () { and one inside your other function to make sure they are running.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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