1

I have array of items ID now i just want that items from custom list using REST API.

array size not fixed in my scenario.

any help will be highly appreciated.

0

You will need to build filter query from array of ids and apply filter as shown below:

/_api/web/lists/getbytitle('SharePoint List Name')/items?$select=Title&$filter=((ID eq 1) or (ID eq 2))

Hope it works for you.

  • i know this way i want any other way this i think not good because i dont know how much ids comes from web so it make url long – Vishal Thakkar Jun 5 '17 at 9:33
  • I don't think any other method will support this kind of filter. /_api/web/lists/getbytitle('SharePoint List Name')/items(1) : It is only allow one value – Samir Khimani Jun 5 '17 at 9:34
0

May be you can create a dynamic CAML query from your array using javascript. Which may be look like below query:

<View><Query>
    <Where>
        <In>
          <FieldRef Name="ID" />
          <Values>
            <Value Type="Number">4</Value>
            <Value Type="Number">6</Value>
            ...
            ...
            ...
          </Values>
        </In>
    </Where>
</Query></View>

And then combine this CAML query with REST. Here CAML query will be act like a filter for your REST Url.

Please look below example that combines CAML query with Sharepoint REST API :

function _rest_Post(rquest) {
    return $.ajax({
        method: rquest.method,
        url: rquest.url,
        contentType: rquest.contentType,
        headers: rquest.header,
        data: JSON.stringify(rquest.body),
    });

}

function GetByCaml(serviceParams) {
    var req = {
        method: 'POST',
        url: url + "/_api/web/lists/getByTitle('" + serviceParams.create.lName + "')/getitems",
        header: {
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose"
        },
        body: serviceParams.create.body,
        contentType: "application/json;odata=verbose",

    };
    return _rest_Post(req);

}

function getData(){
    var queryViewXml = myDynamicQuery; //this will be your dynamically generated caml query
    var params = {};
    params.create= {};
    params.create.lName = "MyList";
    params.create.filter = "";
    params.create.body = {
        'query':{ 
            '__metadata': { 'type': 'SP.CamlQuery' },
            'ViewXml': queryViewXml
        }
    }
    return GetByCaml(params);
};


getData().then(function(data){
    //success handler
}, function(error){
    //failure handler
});
0

Here is one workaround,

var filterQuery = "";

for(var i= 0; i<arrIds.length; i++)
{
    if(i == 0)
    {
        filterQuery ="ID eq "+arrIds[i];
    }
    else
    {
        filterQuery = filterQuery +" or ID eq "+arrIds[i];        
    }
}


loop the ID's in the array & make filter query. Then use it in the url for the API request.

url = _spPageContextInfo.webAbsoluteUrl+"/_api/Web/Lists/GetByTitle('LISTNAME')/Items?$filter="+filterQuery;


May be this will help...

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.