1

How to set value of hidden column using CSR?

Following is the code to set value of a field

(function () {


    var ctx = {};
    ctx.Templates = {};
    ctx.Templates.Fields = {
        'Title': {
            'NewForm': setTitle
        }

    };


    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);


    })();


    function setTitle(ctx) {
        var temp = 'Title 1';
        ctx.CurrentFieldValue =  temp ; //set field value   
        return SPFieldText_Edit(ctx);   
    }
4
  • have you written any code/css to hide your field after it is rendered OR you have made field hide from list settings >> settings for the content type column...?? Aug 9 '17 at 6:52
  • Because if the field is not at all rendering in your view/form you will not be able to access it using CSR. We can only access the ctx fields that are rendered (available in the view/form). Aug 9 '17 at 7:12
  • @Rohit Column is hidden from content type.
    – codemirror
    Aug 9 '17 at 7:38
  • 1
    In this case you will not be able to access your hidden column using ctx. Instead what you can do is make column visible and then hide it using CSR. By this way your column will be available in the form and you will be able to set its value. Aug 9 '17 at 7:48
3

Refer below CSR code to set the default value of the field and hide the field from the form.

(function () {

    var ctx = {};
    ctx.Templates = {};
    ctx.Templates.Fields = {
        'Title': {
            'NewForm': renderTaskCategory
        },

    };
    ctx.OnPostRender = RemoveFields;
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(ctx);
})();

function RemoveFields(){
    var fieldName = "Title Required Field";  // Title of the field that we want to hide (Use inspect element to find the title of the field)
    document.querySelectorAll("[Title='" + fieldName + "']").forEach(function(field){
      GetAncestor( field , 'TR' ).style.display='none';
    });

}
function renderTaskCategory(ctx) {
    var temp = "title 1";
    ctx.CurrentFieldValue =  temp; 
    return SPFieldText_Edit(ctx);
}
2
  • So basically to make it work field should not be hidden from content type but hide from form.
    – codemirror
    Aug 10 '17 at 7:00
  • 1
    Yes you are right. And the reason for that is in CSR you will only be able to access the fields that are rendered or available in the view/form... Aug 10 '17 at 7:21
0

In addition to Rohit Waghela answer

function RemoveFields(){
    var fieldName = "Title Required Field";  // Title of the field that we want to hide (Use inspect element to find the title of the field)
    document.querySelectorAll("[Title='" + fieldName + "']").forEach(function(field){
      GetAncestor( field , 'TR' ).style.display='none';
    });

}

didn't work for me.

So this is jQuery option:

function RemoveFields(){
    var fieldName = "Training";  // Title of the field that we want to hide (Use inspect element to find the title of the field)
    $("[Title='" + fieldName + "']").each(function(){
        $(this).closest("tr").hide();
    });
}

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.