0

I need to set up a complicated display template that's essentially attached to a single field; this field appears on 40+ sites in the same site collection, and I want it to do the same thing on all sites (it's basically a geo-locator field).

I can see two possible ways of doing this (or maybe more if I think harder):

  1. Associate the display template with a Site Column
  2. Associate the display template with a Content Type that contains the site column.

In each case, the relevant list will use the Site Column. So far, just the one column is involved. But let's say for a moment that the requirement grows to three columns, or even more. Then what?

At the moment, I can't even find a way to associate a display template with a Content Type. Is there something I'm missing?

I'm looking for a design principle or two ...

Thanks in advance
Josh

1

Yes, you can attach a CSR script to a Content Type. Content Types have a JSLink property.

Although it is not documented in the Content Type Definition Schema, it is documented in the SPContentType class, and in fact, you can use the JSLink attribute in your CAML Content Type definition. (Or at least, it shows up in Intellisense in VS when creating a Content Type definition...)

As far as what you want to do, though, for me it would boil down to -- do you want your CSR override to always apply to the field, no matter what? Or do you want it to apply to the field when used in a particular content type, but reserve the option to not have the override on the field in some other situation.

If you want to reserve the option to not use the override (or have some other override), then it would make sense to attach the CSR script to the content type. If you know for sure you always want the override applied, no matter what, then it makes more sense to me to attach it to the field. That way, if someone creates a new content type that uses that field, the override will follow.

If you're attaching the CSR scripts on the fields, and your requirement grows to needing overrides on more fields, you can always update the fields at the Site Column level and set the JSLink property to point to the respective override scripts (individual scripts for each field), and then propagate the changes down when you call Update().

If you are attaching the CSR script to the content type, and you need to add overrides for more fields in that content type, it would be as easy as updating the one CSR script to include the additional fields.

  • Dylan, thanks for the insightful opinion. Based on what I know at the moment, the CSR <i>always</i> has to apply to the field. What makes it a yes-able proposition for customers is the notion that they pick up the field and the behaviour automatically follows it. – Josh Korn Nov 1 '16 at 19:26
  • No problem. Yeah, I find that in the vast majority of cases I am attaching the override directly to the field. Either that, or I am attaching to a view to do some extra processing in OnPostRender. I don't think I've ever actually had a case where it made sense to attach it to the content type, but it's good to know that the option is there. – Dylan Cristy Nov 1 '16 at 19:34
  • Dylan, what does a display template for a Content Type look like? Is it just one set of functions per field or something distinctly different? One wrinkle I have to deal with is needing to populate two related fields (a longitude and latitude), and I can't quite see how the geolocator column would know about those unless they were part of the same content type. – Josh Korn Nov 2 '16 at 11:20
  • @JoshKorn the code structure wouldn't be any different. If you have not seen it yet, this is a great reference on CSR overrides. And yes, you are correct in thinking that you wouldn't be able to guarantee those other fields are there unless you attach the script to the content type. You could, however, include conditional code in your script that checks for the existence of those other fields and does something different if they are not there, if that would work in your situation. – Dylan Cristy Nov 2 '16 at 12:27
1

You can attach a jsLink to a Content Type. Content Types have a JSLink property but they can only be used in association with a custom form template.

SPContentType.JSLink property

I would suggest a different approach to handling your situation.

Create a jsLink and associate it with the list like you do normally. Add your site column to the content type and in the jsLink handle the rendering of the column based on the content type. Your code would be something as below:

    (function() {
    var itemCtx = {}; 
     itemCtx.Templates = {};
     itemCtx.Templates.Header = headerTemplate;
     itemCtx.Templates.Item = itemTemplate;
     itemCtx.Templates.Footer = footerTemplate;

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

function headerTemplate(ctx) { 
     var header = RenderHeaderTemplate(ctx); 
     return header;
}

function footerTemplate(ctx) {
     var footer = RenderFooterTemplate(ctx);
     return footer;
}

function itemTemplate(ctx){
     var defItemTemplate;
     var itemId = ctx.ctxId + "," + ctx.CurrentItem.ID + "," + ctx.CurrentItemIdx;
     if (ctx.CurrentItem.ContentType == "ABC")
     {
        //logic to handle column
     }
     else
     {
      defItemTemplate = RenderItemTemplate(ctx);
     }
     return defItemTemplate; 
}
  • Gautham, thanks. At the moment, the requirement is for "anywhere this field appears", so I probably don't need to know which content type is in play. But things do have a habit of changing ... – Josh Korn Nov 1 '16 at 19:28

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.