1

Please let me know which "query" to be used...i just copied a sample code from net... please guide me as I am new to ajax and sharepoint

function getListItems(url, listname, query, complete, failure) {

    // Executing our items via an ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            complete(data); // Returns JSON collection of the results
        },
        error: function (data) {
            failure(data);
        }
    });

}
  • Could you please tell me on what condition you want to fetch list item? I mean based on ID or any particular field value ? Because query/filter will changed accordingly. – Rohit Waghela Apr 6 '16 at 17:06
  • based on ID ,i want to retrieve all fields – user52650 Apr 12 '16 at 6:42
3


Here you can use any of the query but before that you must be clear with your own requirement. If you want to get data with specific parameter (like you want to filter your data) then you can use the following code

Here i am just updating your code to pass the fieldname and fieldvalue. ( here i am assuming your list contain only one record, in case of multiple record you need to iterate the loop)

function getListItems(url, listname,fieldname, fieldvalue, complete, failure) {

    // Executing our items via an ajax request
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$filter= '" + fieldname + "' eq '" + fieldvalue,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
        if(data.length > 0){
                var value1 = data[0].fieldName; // this value1 you can assign it into your textbox
        }},
        error: function (data) {
            failure(data);
        }
    });
    }

Let me know whether this helps you or not.

  • i need to get values from list and fetch that value to textbox...eg my list contains fields admins,name,DOB,ID etc etc...and these values to be fetched to Textboxes whose Id is as :AdminsId,NameId.....so how to link code and textbox Id – user52650 Apr 6 '16 at 9:27
  • I edited my answer again, look at the answer – Aakash Maurya Apr 6 '16 at 9:34
  • running on console shows undefined as result and how to assign value1 to textbox? – user52650 Apr 6 '16 at 9:39
  • how to get lookup field in same code..pls help me out – user52650 Apr 15 '16 at 11:39
  • In order to get value from lookup column you need to first expand the field and then you can get the value. You can use following rest call to expand. url: url + "/_api/web/lists/getbytitle('" + listname + "')/items?$expand='"+ Your lookup column+"'&$select='"+your lookup columnname/fieldWhichYouWantToSelect +"' – Aakash Maurya Apr 16 '16 at 4:39
1

You can build your query in several ways and as per your need. But before coping code you have to understand your requirement as well rest api in 2013. Have a look in my article to get started.

CRUD Operation to List Using SharePoint 2013 Rest API

  • i need to get values from list and fetch that value to textbox...eg my list contains fields admins,name,DOB,ID etc etc...and these values to be fetched to Textboxes whose Id is as :AdminsId,NameId.....so how to link code and textbox Id – user52650 Apr 6 '16 at 9:27
  • Check in my article. It is already there. – Atish Dipongkor - MVP Apr 6 '16 at 9:38
  • thans atish fr ur article but really I m new so ...most of things r bouncer...if u cn provide me code for one field..i will manage fr rest – user52650 Apr 6 '16 at 10:18
0
query ="?$select=ID,Title"
url: _spPageContextInfo.webAbsoluteUrl+ "/_api/web/lists/getbytitle('" + listname + "')/items" + query,

ID and Title are the column names of the SharePoint list specified(list name).

_spPageContextInfo.webAbsoluteUrl gives the url of SharePoint.

In success function you will get the ID,Title etc

    success: function (data) {
             for (var i = 0; i < data.d.results.length; i++) {  
             console.log("Title: "+data.d.results[i].Title);
             console.log("ID: "+data.d.results[i].ID);
}

the above codes iterate in each row of SP List

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.