Get Items From Multiple Line Of Text Column Using CAML Query With SharePoint Rest API - Life Saver

Hey there, in this article we will learn about the CAML query to get items from SharePoint List.
 
Note
When I was stuck up at getting items by filtering multiple line of text column CAML query saved my time, as the title says ;)
 

Introduction

 
CAML Query SharePoint – CAML stands for Collaborative Application Markup Language. It is an XML-based language that is used in SharePoint.
  1. A REST request with a CAML query is usually a POST request
  2. A REST request with a CAML query has always to possess an X-RequestDigest HTTP header and should have the attached CAML query within the request body (and not during a query string).
In this article:
 
We will load HTML fields and buttons dynamically using REST API GET method and on-button click to get list items by filtering multiple line of text column using CAML Query with REST API in 3 simple steps.
 
Prerequisites
  1. SharePoint List with columns 
  2. Add HTML & JS to SiteAssets
  3. Get the code files from my Github 
Screenshot
 
What we will see in this article,
 
Figure 1
Figure 2., Click on the button
 
Figure 3 
 

Code Details

 
STEP 1 - HTML
 
Refer the js files to the HTML 
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4. <title>CAML Query</title>    
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>    
  6.     
  7. </head>    
  8. <body>    
  9.     <div id="Title">    
  10.             
  11.     </div>    
  12. </body>    
  13. <script type="text/javascript" src="../SiteAssets/CAMLQuery.js"></script>    
  14. //This way your scripts get fired only after the DOM containing your id element is already loaded.    
  15. </html>    
STEP 2 - JS
 
On page load, the getItems() function will be called and the HTML structure is loaded dynamically based on the list items!!
  1. getItems();  
  2.   
  3. function getItems() {  
  4.   var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('CAMLQuery')/items?";  
  5.   $.ajax({  
  6.     url: requestUri,  
  7.     type: "GET",  
  8.     headers: {  
  9.       "Accept""application/json; odata=verbose"  
  10.     },  
  11.     async: false,  
  12.     success: function(data) {  
  13.       var items = data.d.results;  
  14.       var htmlVal = '';  
  15.       for (var i = 0; i < items.length; i++) {  
  16.         htmlVal += items[i].Title + "<br/><br/> :" + items[i].Description + ' <br/><br/><button type="button" onclick="getUsingCAML(\'' + items[i].ID + '\',\'' + items[i].Description + '\')">Click Me!</button> <br/><br/><h3 id="' + items[i].ID + '"></h3> ';  //ID sets as dynamic value
  17.       }  
  18.       document.getElementById("Title").innerHTML = htmlVal;  
  19.     },  
  20.     error: function(error) {  
  21.       console.log("getItems :: " + error);  
  22.     }  
  23.   });  
  24. }  
STEP 3
 
On button click, we have passed the id and the MLT column value of the particular ID to filter and get items using CAML Query. 
 
NOTE 
There are FREE tools available in the market to generate CAML query code download and install using the below links,
  1. U2U - CAML Query builder - U2U - Official blog - https://blogs.u2u.be/u2u/post/U2U-Caml-Query-Builder-(Windows-version)-new-version
  2. Saketa - SAKETA
 CAML Query code for our demo app,
  1. <View>    
  2.     <Query>    
  3.         <Where>    
  4.             <Eq>    
  5.                 <FieldRef Name='ColumnName' />" + "    
  6.                 <Value Type='Note'>" + ColumnSearchValue + "</Value>  //Type Note specifies MLT Column  
  7.             </Eq>    
  8.         </Where>    
  9.     </Query>    
  10. </View>    
Now, CAML Query is passed with the MLT search value the REST AJAX returns the value,
  1. function getUsingCAML(ID, Description) {  
  2.   var caml = "<View><Query><Where><Eq><FieldRef Name='Description' />" + "<Value Type='Note'>" + Description + "</Value></Eq></Where></Query></View>";  
  3.   var endpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('CAMLQuery')/GetItems";  
  4.   var requestData = {  
  5.     "query": {  
  6.       "__metadata": {  
  7.         "type""SP.CamlQuery"  
  8.       },  
  9.       "ViewXml": caml  
  10.     }  
  11.   };  
  12.   return jQuery.ajax({  
  13.     url: endpoint,  
  14.     method: "POST",  
  15.     data: JSON.stringify(requestData),  
  16.     headers: {  
  17.       "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
  18.       'content-type''application/json;odata=verbose',  
  19.       'accept''application/json;odata=verbose'  
  20.     },  
  21.     async: false,  
  22.     success: function(data) {  
  23.       var items = data.d.results[0];  
  24.       document.getElementById(ID).innerHTML = "Syntax: <br/>" + items.Syntax;  //Binds the value
  25.     },  
  26.     error: function(error) {  
  27.       console.log(JSON.stringify(error));  
  28.     }  
  29.   });  
  30. }  
That's it!! 
 
Now let us see the CAML Query examples
  1. SharePoint CAML Query Example    
  2.     
  3. Get results from SharePoint List where Id > 1    
  4. <Query>    
  5.     <Where>    
  6.         <Gt>    
  7.             <FieldRef Name=’ID’ />    
  8.             <Value Type=’Counter’>11</Value>    
  9.         </Gt>    
  10.     </Where>    
  11. </Query>    
  12.     
  13. Get results from SharePoint List where Title is not equal to RAM    
  14. <Query>    
  15.     <Where>    
  16.         <Neq>    
  17.             <FieldRef Name=’Title’ />    
  18.             <Value Type=’Text’>RAM</Value>    
  19.         </Neq>    
  20.     </Where>    
  21. </Query>    
  22.     
  23. Get results from SharePoint List where Title begins with RAM    
  24. <Query>    
  25.     <Where>    
  26.         <BeginsWith>    
  27.             <FieldRef Name='Title' />    
  28.             <Value Type=’Text’>RAM</Value>    
  29.         </BeginsWith>    
  30.     </Where>    
  31. </Query>    
  32.     
  33. Get results from SharePoint List where Title contains RAM    
  34. <Query>    
  35.     <Where>    
  36.         <Contains>    
  37.             <FieldRef Name='Title' />    
  38.             <Value Type=’Text’>RAM</Value>    
  39.         </Contains>    
  40.     </Where>    
  41. </Query>    
  42.     
  43. Get results from SharePoint List where Id Lower than or equal to 23    
  44. <Query>    
  45.     <Where>    
  46.         <Leq>    
  47.             <FieldRef Name='ID' />    
  48.             <Value Type=’Counter’>23</Value>    
  49.         </Leq>    
  50.     </Where>    
  51. </Query>    
  52.     
  53. Get results from SharePoint List where Title is not null    
  54. <Query>    
  55.     <Where>    
  56.         <IsNotNull>    
  57.             <FieldRef Name='Title' />    
  58.         </IsNotNull>    
  59.     </Where>    
  60. </Query>    
  61.     
  62. CAML Query to Limit Row    
  63.     
  64. <Query>    
  65.     <View>    
  66.         <RowLimit>10</RowLimit>    
  67.     </View>    
  68. </Query>    
Hooray!! Happy CAML riding!