2

I'm following this example for using client-side rendering for my edit form. I've modified it to make the Title not editable and I want to have a "fancy" rendering for a manged metadata column.

(function () { 
    var customContext = {}; 
    customContext.Templates = {}; 
    customContext.Templates.Fields = { 

        "Title": { 
            "EditForm": readonlyFieldTemplate 
        },
        "MyTermSet":{
            "EditForm": fancyMultiTerm
        }
    }; 

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(customContext); 

})();

function fancyMultiTerm(ctx){
    var termset = ctx.CurrentFieldSchema.TermSetId;

    // do more stuff
    ......
    return fancyTaxonomyControl;
}

And this is working...but it isn't very portable.

Every time I want to use a fancy multiterm taxonomy control I have to change the name of the column.

Is there a way to create a client side render file that will look through all the fields that are on the page and use the custom renderer for all taxonomy fields?

Or to put it another way, is there some way to make this work when I don't know the name of the column/field in advance?

I'm looking for new and edit forms, as well as list views.

2

The global ctx object is available before you call the RegisterTemplateOverrides function.

So you can create the Templates definition based on the fields in ctx.ListSchema.Field


SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() {
  function init() {
    console.clear();

    var customContext={
      OnPreRender: function(ctx) {},
      Templates: {
        Fields: {},
      },
      OnPostRender: function(ctx) { },
    };

    console.info(ctx);

    ctx.ListSchema.Field.forEach(function(field){
      console.info(field);
      customContext.Templates.Fields[field.Name]={
        View: function(ctx) { return "21"; },
        //             EditForm: function(ctx) { return ""; },
        //             DisplayForm: function(ctx) { return ""; },
        //             NewForm: function(ctx) { return ""; }
      }
    });

    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(customContext);
  }

  RegisterModuleInit(SPClientTemplates.Utility.ReplaceUrlTokens("~siteCollection/Style Library/myCSRfile.js"), init);
  init();

});

And of course you use the Cisar Chrome Extension to WYSIWYG edit all this CSR

iCSR

  • Finally got around to needing this. No matter what I try I'm not able to access ctx.ListSchema. Any suggestions? – Rothrock Aug 2 '17 at 20:40
1

There actually is a way. Instead of using the internal field name, like:

function editOverride(ctx) {
    // render edit interface
}

function displayOverride(ctx) {
    // render display overrides
}

// override the render for the field called Url?
var overrides = {
    Templates: {
        Fields: {
            MyInternalFieldName: {
                NewForm: editOverride,
                EditForm: editOverride,
                DisplayForm: displayOverride,
                View: displayOverride,
            }
        }
    }
};

// register the template overrides, and to keep it simple, ignoring MDS
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrides);

you can specify overrides by field type instead:

function editOverride(ctx) {
    // render edit interface
}

function displayOverride(ctx) {
    // render display overrides
}

// override the render for the field called Url?
var overrides = {
    Templates: {
        Fields: {
            Text: {
                NewForm: editOverride,
                EditForm: editOverride,
                DisplayForm: displayOverride,
                View: displayOverride,
            }
        }
    }
};

// register the template overrides, and to keep it simple, ignoring MDS
SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overrides);

This will replace the default renderer for all fields of type Text on any form on which it is loaded. This works on SharePoint 2013 through SharePoint Online.

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.