0

I'm completely new to jquery and by now I'm not able to use it. Can anyone help to write a piece of code, which will display single item (given ID) from the list 'CounterList', column 'Counter' to a content editor. Main goal is to enable formatting the output displayed in the content editor with the html tags. Thanks!

0
<script type="text/javascript">
 ExecuteOrDelayUntilScriptLoaded(queryListItems, "sp.js")

   var itemId = 2;   // Set your listitem id
   var counterListItem;

  function queryListItems(){
     var clientContext = new SP.ClientContext(); 
     var counterList = clientContext.get_web().get_lists().getByTitle('CounterList');
     counterListItem = counterList.getItemById(itemId);
     clientContext.load(counterListItem);
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
   }

   function onQuerySucceeded() { 
       alert(counterListItem.get_item('Counter')); // Change column name

   }

   function onQueryFailed(sender, args) {
     alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
   }
   </script>
  1. Copy code in txt file.
  2. Upload file to document library and copy path of file.
  3. Edit content editor webpart, paste path in Content link textbox and save it.
  4. You will get listitem value based on ID
  5. Now you can add html tags after script tag and set listitem values in div using div.innerHTML instead of alert through javascript.
4
  • Why do you use Function.createDelegate? – Danny '365CSI' Engelman Apr 16 '16 at 8:31
  • The createDelegate function is useful when setting up an event handler to point to an object method that must use the this pointer within its scope. – Viraj Gorajia Apr 16 '16 at 8:39
  • So why do you use it here? – Danny '365CSI' Engelman Apr 16 '16 at 8:40
  • You can also write clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed); – Viraj Gorajia Apr 16 '16 at 8:41

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.