Get list item data from SharePoint list using Javascript snippet added as WebPart

I am using the below code as a basis to allow the end user to enter a last name and / or id and it just pulls all instances of that last name / id from the list and puts them in a text box or something. It's on the SharePoint 2013 page.

<input type="text" id="Surname" name="Surname" style="width:200px" value="Enter the surname" onfocus="this.value = '';">
<input type="text" id="IDNumber" name="IDNumber" style="width:200px" value="Enter ID number" onfocus="this.value = '';">
<input type="button" name="button" value="Find Services used by person" onclick="findDocuments()">
<script type="text/javascript">
function findDocuments()
{
    var IDNumber = document.getElementById("IDNumber").value;
    var Surname = document.getElementById("Surname").value;
    window.open("ADDRESS HERE" + Number + "-FilterField2%3DSurname-FilterValue2%3D" + Surname)        
}

</script>

      

Note. ADDRESS HERE is the address of the list or library and I don't want it to necessarily open a window, but to insert the extracted data and put it in a text box on the page.

To reiterate with greater clarity, the user will see one or two fields, one field for entering an ID number and one field for entering a last name (or both). When they click a button, it will look at how many instances of that ID or last name are in the list and return the number of different services that person has used and put them in the box.

At the moment I can only use Media Content websites to do this as I have no experience or resources to install Visual Studio or anything similar. Anyone got any suggestions on this?

Thank.

+3


source to share


1 answer


You can use REST service to fetch list items using ajax using the following parameters:



$.ajax({
     url:"/_api/lists/getbytitle('YOUR_LIST_NAME')/items?$select=FIELD1,FIELD2&$filter=FIELD1 eq '" + IDNumber + "' and FIELD2 eq '" + Surname + "'",
     headers: { "Accept": "application/json; odata=verbose"},
     success:function(data) {
            $.each(data.d.results, function(i, item) {
                  output += data.d.results[i].FIELD1 + " - " + data.d.results[i].FIELD2;
            })
     }
});

      

0


source







All Articles