JSLink is a way to attach custom JavaScript code to run when the object it is attached to is shown on the page. For example, if setting the JSLink property on a field, the JS file(s) will be executed whenever that field is shown in a view or in a form.
JSLink property can be set on:
- Fields
- Content Types
- Views
Although the JSLink property can be specified in a list schema for the list forms, the JSLink value gets attached to the form’s web part instead of being a property.
Coming back to fields, content types and views, JSLink property can be set declaratively in the XML and can also be changed later with REST and JSOM. This article will show you how to set or change the JSLink property of a list content type (copy of a content type applied to a list). You can run this in Chrome Console on a SharePoint page:
fetch("https://webAbsoluteUrl/_api/lists/getbytitle('List Title')/contenttypes('<content_type_id>')",
{"method": "POST",
"credentials": "include",
"body": JSON.stringify({"__metadata": { "type": "SP.ContentType" }, "JSLink": "~sitecollection/Style Library/js/file.js?rev=2017050801"})
"headers":
{"content-type":"application/json;odata=verbose",
"accept": "application/json;odata=verbpse",
"X-RequestDigest": document.querySelector('#__REQUESTDIGEST').value}
})
Versions of SharePoint: 2013, 2016