4

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.

6
  • Are you unsure of how to get values from list using JSOM? or just this get_fieldValuesAsTest?
    – Asad Refai
    Commented Aug 6, 2015 at 6:06
  • I am unsure how to use get_fieldValuesAsTest. And I can get values using JSOM.
    – Serhiy
    Commented Aug 6, 2015 at 6:09
  • 1
    What code have you written till now? Can you share?
    – Asad Refai
    Commented Aug 6, 2015 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
    Commented Aug 6, 2015 at 7:08
  • 2
    For one server request, best way would be to use REST api. http://siteurl//_api/web/lists/getbytitle('listitle')/item(itemId)/fieldvaluesastext
    – Akhoy
    Commented Aug 6, 2015 at 7:15

1 Answer 1

6

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());
});
5
  • Thanks! This example IS working. Maybe you have an idea how to download values of ALL items with only one clientContext.load?
    – Serhiy
    Commented Aug 6, 2015 at 7:05
  • 1
    @Serhiy I've updated the code, you can check if it meets your needs. Commented Aug 6, 2015 at 7:33
  • Man, that's exactly what I was looking for! Thank you so much!
    – Serhiy
    Commented Aug 6, 2015 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? Commented Aug 18, 2016 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. Commented Aug 22, 2016 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.