How to include custom fields in clientContext.load()?

Loading some list items via

var lists = web.get_lists();
var list = lists.getByTitle(listTitle);
var listItems = [];
for (var i in len) {
listItems[i] = list.getItemById(itemIds[i]);
context.load(listItem[i], ‘Title’);
}
context.executeQueryAsync(function () {
for (var i in len) {
console.log(listItems[i].get_fieldValues());
}
}, function () {
});

I can get the right items easily, logging their titles using .get_fieldValues().
However, if I try to include custom fields in context.load() the request fails. I’ve tried a few different syntax versions I’ve found when looking for an answer:

context.load(listItem[i], ‘Title,Custom’);
context.load(listItem[i], [‘Title’, ‘Custom’]);
context.load(listItem[i], ‘Include(Title,Custom)’);

However, none of them works. What’s the correct syntax to get custom fields?

If it matters, the custom field isn’t a required field and not all the list entries have a value for it.

=================

  

 

This post might help you sharepoint.stackexchange.com/questions/93595/…
– nbi
Jan 15 ’15 at 17:53

  

 

@nitish Thank you for your time, but that post is about C#. This post is about JSOM, the Javascript object model.
– Nit
Jan 15 ’15 at 17:56

=================

1 Answer
1

=================

If you’re dealing with a collection of items (example all of the items in a list, or all of the lists in a web), you need to use:

clientContext.load(collOfItems, ‘Include(Field1,Field2)’);

For single items (like an individual site, list, or item) you need to use a comma seperated list of string arguments after the item to load, like:

clientContext.load(singleItem, ‘Title’, ‘Id’);

or an array of string arguments like

clientContext.load(singleItem, [‘Title’, ‘Id’, …]);

(thanks Nit)

If you’re not getting the correct values, I find the most common problem is that you’re not using the correct internal name of the field. In which case you should use your browser dev tools to inspect the names of the list item fields after you load them with something like…

collOfItems.getItemAtIndex(0).get_fieldValues()

Internal names (particularly those with spaces) are encoded, and if you have changed the field name in the UI the object models still generally reference them by the original name. You can also do something like create a list view in the UI, then inspect it using SharePoint Designer to get a better picture of the right field names in cases (like yours) where they may not display with a regular load.

Worth adding as a comment here; a more common pattern than loading items by an index id (like in your question) is to load all of your list items with something like list.getItems(SP.CamlQuery.createAllItemsQuery()) then use the load statement I outline above for collections, like this:

// I like to just use an empty string instead of a query object 😛 same results
// if you’re not using folders
var items = list.getItems(“”);
clientContext.load(items, “Include(Title,Custom)”);
clientContext.executeQueryAsync(…);

  

 

Thank you for the answer, it was indeed a problem with the internal name. However, why do you suggest loading all the items? Especially if it’s a big list, wouldn’t it be better to only load the ones that you want?
– Nit
Jan 15 ’15 at 18:14

  

 

You can change the Query object to retrieve the ones you want, but then you have to figure out CAML or get help from a tool. I suppose if you’re only going to load a small number then your method will work fine; CAML allows you to use criterea other than the ID, which I find I often cannot guess ahead of time though.
– John-M
Jan 15 ’15 at 18:18

2

 

Just so you know, I tested, and you can also pass an array of single arguments to .load when working with single items: clientContext.load(singleItem, [‘Value1’, ‘Value2’]);
– Nit
Jan 15 ’15 at 19:55