Question

I'm fairly new to the JavaScript Object Model (JSOM) and I want to do a simple call to get a field value. Very easy in C#, but I can't seem to get a field value even though I have the ListItem:

overrideContext.Templates.Item = customItem;
function customItem(context) {
var title = context.CurrentItem["Title"];
var img = "<img src='" + getImageUrl(1) +  "' />";
return title + img;

function getImgUrl(classID) {
var clientContext = new SP.ClientContext();
var web = clientContext.get_web();
var sourceList = web.get_lists().getByTitle('ClassifiedImages');
var listItem = sourceList.getItemById(classID);


clientContext.load(listItem);
clientContext.executeQueryAsync(Function.createDelegate(this, function () { _returnParam = onSuccess(); }), Function.createDelegate(this, function () { _returnParam = onFail(); }));

return _returnParam;
}

I don't want to use an Async execution. I need the value to be returned immediately. Since I have the listItem, why can't I just say return listItem["Title"]; ??

Was it helpful?

Solution

Natively CSOM only allows for asynchronous calls. If you want to overcome that, you can use a framework such as http://spservices.codeplex.com/

OTHER TIPS

You only have a reference to the list item, but the actual list item is not fetched to you from the server until you do the executeQueryAsync.

Not until then do you have any actual information to work with.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top