0

I want to search the value in one particular column of the list using RestAPI.

_api/web/lists/getbytitle('FAQ')/items?$filter=contains('" + Searchtext + "',Answers)

And tried this,

_api/web/lists/getbytitle('FAQ')/items?$filter=substringof('" + Searchtext + "',Answers) 

but this is also not worked. (And the column of Answers type is multiline of text) Can any one please suggest me answer? Is it possible to filter the mulitiline of text column in list ?

0

We can use CAML Query in REST API to achieve it.

The following example code for your reference.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
    var Searchtext="BCD";
    var listTitle="FAQ";
    var queryViewXml="<View><Query><Where><Contains><FieldRef Name='Answers'/><Value Type='Note'>"+Searchtext+"</Value></Contains></Where></Query></View>" ;
    getListItems(listTitle, queryViewXml).done(function(data){
        $.each(data.d.results,function(i,item){
            console.log(item.Title);
        });
    });
});
function getListItems(listTitle, queryViewXml){
    var queryPayload = {
        'query':{ 
            '__metadata': { 'type': 'SP.CamlQuery' },
            'ViewXml': queryViewXml
        } 
    }; 
    var endpointUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('" + listTitle + "')/getitems";
    return $.ajax({
        type: "POST", 
        headers: { 
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "X-RequestDigest": $("#__REQUESTDIGEST").val()
        }, 
        data: JSON.stringify(queryPayload),
        url: endpointUrl        
    });  
}
</script>
  • thanks for your replay, can we use this filter with out CAML? – Bavya Rajendran Apr 9 at 11:34
  • I have to install jquery for this right? because it showing error like "Cannot find name '$'. Do you need to install type definitions for jQuery? Try npm i @types/jquery." – Bavya Rajendran Apr 9 at 11:39
  • The multiple text field can't use in $filter in REST API, if you want to use the jQuery in SPFx, please use npm install jquery@2. Refer to: docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/… – LZ_MSFT Apr 10 at 1:18
  • Facing issue like "Failed to load resource: the server responded with a status of 403 ()" here i have attached the image [sharepoint.stackexchange.com/questions/260849/… – Bavya Rajendran Apr 11 at 14:21
  • Hey hi instead of POST i gave the GET, but it is fetching all data from the list. can u tell me what is my mistake? – Bavya Rajendran Apr 11 at 14:44
0

Using this in ReactJS i got the answer with out using jquery,

      import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';

        export function getSearch(Searchtext,absoluteUrl,contextVal){
            let camlQueryPayLoad = {
                        'query':
                        {
                            '__metadata':
                                { 'type': 'SP.CamlQuery' },
                            "ViewXml": "<View><Query><Where><Or>
<Contains><FieldRef Name='Answers' /><Value Type='Note'>" + Searchtext + "</Value></Contains>
<Contains><FieldRef Name='Title' /><Value Type='Text'>" + Searchtext + "</Value></Contains>
</Or></Where></Query><ViewFields><FieldRef Name='Answers' /><FieldRef Name='ID' /><FieldRef Name='Title' /></ViewFields><QueryOptions /></View>"
                        }
                    };


            contextVal.post(`${absoluteUrl}/_api/web/lists/getbytitle('FAQ')/GetItems`, SPHttpClient.configurations.v1, spOpts)
                .then((response: SPHttpClientResponse) => {
                    if (response.ok) {
                        response.json().then((response) => {
                            let jsonSearchGetList = { type: "FAQViewAll_Search", response: response.value };
                            console.log("FAQViewAll_SearchValueActionVal:", response);
                            Dispatcher.dispatch(jsonSearchGetList);
                        })
                    }
                })
                .catch(error => {
                    console.log("FAQViewAllSearchResponseError", error.response);
                        })
        }

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.