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!
1 Answer
<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>
- Copy code in txt file.
- Upload file to document library and copy path of file.
- Edit content editor webpart, paste path in Content link textbox and save it.
- You will get listitem value based on ID
- Now you can add html tags after script tag and set listitem values in div using div.innerHTML instead of alert through javascript.
-
Why do you use
Function.createDelegate?Danny '365CSI' Engelman– Danny '365CSI' Engelman2016-04-16 08:31:51 +00:00Commented Apr 16, 2016 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– Viraj Gorajia2016-04-16 08:39:28 +00:00Commented Apr 16, 2016 at 8:39
-
So why do you use it here?Danny '365CSI' Engelman– Danny '365CSI' Engelman2016-04-16 08:40:36 +00:00Commented Apr 16, 2016 at 8:40
-
You can also write clientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);Viraj Gorajia– Viraj Gorajia2016-04-16 08:41:01 +00:00Commented Apr 16, 2016 at 8:41