3

I need to get items using JSOM and show them on a page in a display form. So currency field will be shown like $ 123,456.78 the date will be formatted accordingly to settings and Yes\No value will be displayed correctly.

I gues, get_fieldValuesAsText is what can help me best, but I can't understand how to use it and can't find any example.

  • Are you unsure of how to get values from list using JSOM? or just this get_fieldValuesAsTest? – Asad Refai Aug 6 '15 at 6:06
  • I am unsure how to use get_fieldValuesAsTest. And I can get values using JSOM. – Serhiy Aug 6 '15 at 6:09
  • 1
    What code have you written till now? Can you share? – Asad Refai Aug 6 '15 at 6:10
  • Asad, Xiaodan Mao has already added a working example, however I am still searching for a way to download all items with one server request. – Serhiy Aug 6 '15 at 7:08
  • 1
    For one server request, best way would be to use REST api. http://siteurl//_api/web/lists/getbytitle('listitle')/item(itemId)/fieldvaluesastext – Akhoy Aug 6 '15 at 7:15
5

Here is an example:

var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("Title");
var item = list.getItemById(id);
var fieldValuesAsText = item.get_fieldValuesAsText();

clientContext.load(fieldValuesAsText);
clientContext.executeQueryAsync(function (sender, args) {
    var fieldValues = fieldValuesAsText.get_fieldValues(); // Text values of all fields
    var fieldValue = fieldValuesAsText.get_item('Title'); // Text value of one field
}, function (sender, args) {
    alert(args.get_message());
});

Update, get field values as text of all items in one executeQueryAsync call:

var clientContext = SP.ClientContext.get_current();
var list = clientContext.get_web().get_lists().getByTitle("Title");
var items = list.getItems(new SP.CamlQuery());

clientContext.load(items, 'Include(FieldValuesAsText)');
clientContext.executeQueryAsync(function (sender, args) {
    var enumerator = items.getEnumerator();

    while (enumerator.moveNext()) {
        var item = enumerator.get_current();
        var fieldValuesAsText = item.get_fieldValuesAsText();
        var fieldValues = fieldValuesAsText.get_fieldValues();
        console.log(fieldValues);
    }
}, function (sender, args) {
    alert(args.get_message());
});
  • Thanks! This example IS working. Maybe you have an idea how to download values of ALL items with only one clientContext.load? – Serhiy Aug 6 '15 at 7:05
  • 1
    @Serhiy I've updated the code, you can check if it meets your needs. – Xiaodan Mao Aug 6 '15 at 7:33
  • Man, that's exactly what I was looking for! Thank you so much! – Serhiy Aug 6 '15 at 7:35
  • How could you use this to get one fieldValue as text (a lookup column), and then still pull in other columns normally? – Sabien Ambrose Aug 18 '16 at 16:30
  • @SabienAmbrose I don't have a good solution. But maybe you can load twice, load other columns first, then load that field as text, and then execute the query. – Xiaodan Mao Aug 22 '16 at 1:54

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.