0

I would like to know if there is a way to get all items without referring one by one like I have done in my code? This code returns nothing.

function onS() {
    var listString = "";
     var Enum = listItems.getEnumerator();
       while (Enum.moveNext()) {
     var currentItem = Enum.get_current();
    var data = currentItem.get_fieldValues();
    listString += currentItem.Title;
    }
    $("#items").html(listString);
}
4
  • 1
    Please elaborate. You want to build your listString variable without having to enumerate through the collection of items? If so, why?
    – Submits
    Commented Sep 10, 2015 at 8:51
  • I have tried in many different ways but nothing works
    – abc
    Commented Sep 10, 2015 at 10:05
  • I did but it returns nothing
    – abc
    Commented Sep 10, 2015 at 10:48
  • Sorry, it is "data.Title". The whole JSON response resides in data Commented Sep 10, 2015 at 13:49

2 Answers 2

3

if you want to get the JSON response of all field values use get_fieldValues() method

var currentItem = Enum.get_current();
 var data = currentItem.get_fieldValues();

it returns the keyvalue pair JSON data like

{'Title':'Title Value',
 'ID':'ID value',
 'ColumnInternaName':'ColumnValue',
  .....
  .....}

you can get the title by

data.Title

Id by

data.ID

etc...

if you load items using 'Include' like

context.load(listItems,'Include(Title,ID)')

Then the JSON only contains the data of Title and ID

4
  • not Title(), it is just Title, it is not a function Commented Sep 10, 2015 at 9:50
  • what error did you get? update your code in Question Commented Sep 10, 2015 at 9:51
  • I have updated my code , take a look please
    – abc
    Commented Sep 10, 2015 at 10:10
  • answered below you question Commented Sep 10, 2015 at 10:14
1

I am mostly using REST to get all items from a list like the code below:

<script type="text/javascript">

                $(document).ready(function() {    

                    // Rest Call
                    var requestUri = "/_api/lists/getbytitle('News')/items";

                        $.ajax({
                            url: requestUri,
                            type: "GET",
                            headers: { "ACCEPT": "application/json;odata=verbose" },
                            success: function (data) {
                                $.each(data.d.results, function (i, item){
                                    var title = item.Title;
                                    var description = item.Description;
                                    var pictureURLDesc = item.PictureURL.Description;
                                    var pictureURL = item.PictureURL.Url;   
                                    var linkDesc = item.Link.Description;
                                    var linkURL = item.Link.Url;    
                                    var currentItemID = item.ID;                

                                    try 
                                    {   
                                        // Slice description
                                        if (description.length > 102){
                                            llength = description.slice(0, 102) + " ...";
                                        }
                                        else {
                                            llength = description;
                                        }
                                    } 
                                    catch ( err ) 
                                    { 
                                        alert( err ); 
                                    }               

                                    // Check News item   
                                    alert("Item Nr: " + currentItemID + "Title: " + title + "Description: " + llength);                 
                                })
                            },
                            error: function () {
                                alert("Error getting items");
                            }                     
                        });   
});                              
</script>

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.