0

On the project I am working on (SP2013 on-premise), almost all the form pages (Newform.aspx, DispForm.aspx and Editform.aspx) have added a Script editor webpart with JQuery to customize form layout (e.g. disable/hide/prefill some fields). Each of the form pages contains 20 lines of JQ.

Should I consolidate all JQ into single JS file? The JS file will be stored under ~site collection/style library/JS/. Then the site's Master page will include this JS file. The JS file will contains logic like if this page url = xxxx/xxxx/ then process this part...

The advantage I can think of:

  1. Version control is avaiable
  2. Single place update/deployment. No need to go over every page
  3. JS functions reusable. Hence lines of code can be reduced.

Disadvantage I can think of:

  1. Functions and variables may conflict each other if not handle carefully.
  2. Performance(?) reduced since every page load entire JS file.

What do you think?

1

Instead of separate file you can also use CSR/JSLink here. That way you can consolidate all the function for each of the form in one JavaScript file. You simply need to use JSLink option for those pages(Newform.aspx, DispForm.aspx and Editform.aspx).

Here is the examples for JSLink(CSR):

Client-side rendering (JS Link) code samples

Using JSLink to change the UI of a SharePoint list/view

Great documentation here:

SharePoint 2013 Client Side Rendering: List Views

Introduction to Client-Side Rendering in SharePoint 2013

Using CSR API

Main thing you need to consider here is below snippet:

var options = {
   OnPreRender: /* function or array of functions */,
   Templates: {
     View: /* function or string */,
     Body: /* function or string */,
     Header: /* function or string */,
     Footer: /* function or string */,
     Group: /* function or string */,
     Item: /* function or string */,
     Fields: {
       'Field1 Internal Name': {
           View: /* function or string */,
           EditForm: /* function or string */,
           DisplayForm: /* function or string */,
           NewForm: /* function or string */
       },
       'Field2 Internal Name': {
           View: /* function or string */,
           EditForm: /* function or string */,
           DisplayForm: /* function or string */,
           NewForm: /* function or string */
       },
       // .... and so on
     }
   },
   OnPostRender: /* function or array of functions */
 };

So as per the code snippet you can specify your function for each views or forms here(centrally managed).

All your advantage you specified will be available and also in disadvantage, you will not face problem in performance because this are the default SharePoint CSR API functions which was overridden and used here.

Also there won't be any problem with conflict issues as all your code is inside one file that you can easily manage.

Only thing you need to do here is open each page one by one and then add JS Link Property with JavaScript file reference. Though this can be disadvantage, I would prefer this API for custom views, forms rendering.

Hope this helps!

  • The 2nd link url should be this? sharepointnutsandbolts.com/2013/01/… – Mark L May 19 '15 at 1:19
  • It seems I need to make sure all lists do not use same "field" name? – Mark L May 19 '15 at 2:00
  • Answer 1: Yes, That's the link. I have updated the answer. Answer 2: You need to have one common JS file for one list different views or forms. For each and every list you should have one JS file. If the fields are the same for each list then you can go ahead with One common JS file. – Pradip R. May 19 '15 at 8:57
  • I would also reccomend Andrey Makeev's tutorial. It's one of the best tutorials on CSR there is. codeproject.com/Articles/620110/… – Denis Molodtsov May 25 '15 at 10:16
1

Consolidate into one file. Advantages are big, and disadvantages don't really exist, as you have full control of the functions/variables (sure, you can make mistakes, but you can do that otherwise as well), and browsers cache the JS file, but remember to enable BlobCache.

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.