Set JSLink on a Site Column With JSOM

I remain very impressed with JSLink and what it can do, particularly related to individual site columns.  While preparing a presentation for the local Office 365 Saturday event here in Brisbane, Queensland I realised that while it is easy to connect a JSLink script to a site column using PowerShell, there have been many occasions on client sites where I have been limited to Site Collection administrator access permissions with no access to a server.

While I live in hope that one day Microsoft might add that simple text field to the properties of a site column in the browser, I figured it must be possible using JavaScript with JSOM.

And of course it is . . .


var context = new SP.ClientContext.get_current();
var web = context.get_web();
var field = web.get_availableFields().getByTitle("My Site Column");
context.load(field);
context.executeQueryAsync(function(){
field.set_jsLink("~sitecollection/Style Library/custom/MyJSLink.js");
field.updateAndPushChanges(true);
context.executeQueryAsync(function(){
var jsl = field.get_jsLink();
console.log(jsl);
});
});

This can be run from the console in a browser – just make sure you use your site column name and set the path to your script. You do also need to be logged in as a Site Collection administrator. Oh, and it works for Office 365 too.

There is also a copy on GitHub if you prefer.

Happy JSLinking!

This entry was posted in JSLink, Office 365, SharePoint and tagged , , , , , , . Bookmark the permalink.

2 Responses to Set JSLink on a Site Column With JSOM

  1. Nandalal Ramphal says:

    Can you layout the steps to this, i have tried powershell with no luck, like what does console in a browser mean? again sorry for the nood questions

    • paylord says:

      Hello Nandalal

      PowerShell is definitely the way to go if you have access but if you click F12 on your browser then it will open the developer tools either as a section at the bottom of the window or as a separate window. Note that some organisations disable this. In developer tools you will find a tab for console which lets you monitor messages sent to the console but you can also interact with the page using JavaScript. That is where you need to paste and run the script.

      Good luck.

      Dave

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s