1

I am new to CAML queries and javascript but I would like to display a result from a SharePoint list query in HTML. My list has approximately 300 total rows and I would like to get a row count when the Title field contains certain text to display in HTML. The result would be text on the page that reads "There are x records that begin with ..."

My query seems to work but I don't know how to get the query to run on page load to display.

<View><Query><Where><BeginsWith><FieldRef Name='Title' /><Value Type='Text'>AFI</Value></BeginsWith></Where></Query></View>

Once I get this sorted out I would like to also have a few more queries to get the row counts of different query conditions. "Rows that begin with ABC - xx" "Rows that begin with 123 - xx" and so on...

UPDATE: Here is where I am right now and getting Uncaught SyntaxError: Unexpected identifier and Invalid or unexpected token

<script src="/sites/AFA1/SiteAssets/Scripts/jquery-3.2.1.min.js"></script>
<script type="text/javascript" language="javascript" src="/sites/AFA1/SiteAssets/Scripts/jquery.SPServices.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
    getListItems(webUrl,listTitle) 
{
var webUrl = '/sites/AFA1/PMS'
var listTitle = 'Milestone%200%20DataSheet'
var viewXml = '<View><Query><Where><BeginsWith><FieldRef Name='Title' /><Value Type='Text'>AFI</Value></BeginsWith></Where></Query></View>';
    var url = webUrl + "/_api/web/lists/getbytitle('"+listTitle+"')/items"; 
    var queryPayload = {  
               'query' : {
                      '__metadata': { 'type': 'SP.CamlQuery' }, 
                      'ViewXml' : viewXml  
               }
    };

    return $.ajax({
           url: url,
           method: "POST",
           data: JSON.stringify(queryPayload),
           headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "Accept": "application/json; odata=verbose",
              "content-type": "application/json; odata=verbose"
           }
     });
}
});

</script>

1 Answer 1

1

I use this script to get items from list with CAML. You can use the data result object to get the count etc.

function getListItems(webUrl,listTitle) 
{
    var viewXml = '<View><Query><Where><BeginsWith><FieldRef Name='Title' /><Value Type='Text'>AFI</Value></BeginsWith></Where></Query></View>';
    var url = webUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems"; 
    var queryPayload = {  
               'query' : {
                      '__metadata': { 'type': 'SP.CamlQuery' }, 
                      'ViewXml' : viewXml  
               }
    };

    return $.ajax({
           url: url,
           method: "POST",
           data: JSON.stringify(queryPayload),
           headers: {
              "X-RequestDigest": $("#__REQUESTDIGEST").val(),
              "Accept": "application/json; odata=verbose",
              "content-type": "application/json; odata=verbose"
           }
     });
}
5
  • Thanks Danny. What would be the best way to fire off the function automatically when the page loads? I've only used buttons to start the functions before. Commented Oct 9, 2017 at 12:58
  • Check this: learn.jquery.com/using-jquery-core/document-ready Commented Oct 9, 2017 at 12:59
  • I can't seem to get anything to post back. I'd include my code but I've deleted everything out of frustration. Commented Oct 9, 2017 at 18:50
  • I got my example from: sharepoint.stackexchange.com/questions/134860/… and stackoverflow.com/questions/26884647/… Commented Oct 10, 2017 at 11:40
  • Once I get a working sample I will be able to get the rest going but I keep getting errors Commented Oct 10, 2017 at 17:01

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.