Tuesday, December 20, 2016

Set JSLink in List View using JavaScript Object Model (ECMA) Programming In SharePoint 2016 And Office 365




Steps:

  • Add Content Editor Web part (CEWP) to the SharePoint page.
  • Save the below script as a text file and upload it to Site Assets page.
  • Refer the script file from the CEWP.

Script:

<script language="javascript" type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
 <script language="javascript" type="text/javascript">
 $(document).ready(function() {
 SP.SOD.executeFunc('sp.js', 'SP.ClientContext', setJSLink);
 });
var oView;
function setJSLink() {
 //Get the client context,web and list object
 var clientContext = new SP.ClientContext();
 var oWebsite = clientContext.get_web();
 var oList = oWebsite.get_lists().getByTitle('Products');
 //Get the view object and set the jsLink property
 oView = oList.get_views().getByTitle('CustomProductView');
 oView.set_jsLink("~site/siteassets/ListViewModification.js");
oView.update();
 //Load the client context and execute the batch
 clientContext.load(oView);
 clientContext.executeQueryAsync(QuerySuccess, QueryFailure);
}
function QuerySuccess() {
 console.log("JSLink has been set to the view.");
}
function QueryFailure(sender,args) {
 console.log('Request failed'+ args.get_message());
}
</script>

No comments:

Post a Comment