Question

I have a long $filter query like ID eq 1 or ID eq 2 or ID eq 33 or ID eq ... and it works like a charm when we have up to 159 conditions joined by or.

But, when I add one more condition (started from 160) I have an error:

{"error": {
"code": "-2147024809, System.ArgumentException",
"message": {
  "lang": "en-US",
  "value": "Value does not fall within the expected range"
}}}

There is no problem with ID - condition can be like ID eq 1 or ID eq 1 ... - the issue occupied only with count of conditions.

In SP logs I see following:

    System.ArgumentException: Value does not fall within the expected range, StackTrace:   
in Microsoft.SharePoint.SPListItemCollection.EnsureListItemsData()    
in Microsoft.SharePoint.SPListItemEntityCollection.TryWriteAsOData(ServerStub serverStub, ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext)    
in Microsoft.SharePoint.Client.ServerStub.Write(Object value, Uri path, ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext)    
in Microsoft.SharePoint.Client.Rest.RestRequestProcessor.Process()    
in Microsoft.SharePoint.Client.Rest.RestRequestProcessor.ProcessRequest()    
in Microsoft.SharePoint.Client.Rest.RestService.ProcessQuery(Stream inputStream, IList1 pendingDisposableContainer)    
in Microsoft.SharePoint.Client.ClientRequestService.ProcessRestQuery(Stream inputStream)    
in SyncInvokeProcessRestQuery(Object , Object[] , Object[] )    
in System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)    
in System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)    
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)    
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)    
in System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)    
in System.ServiceModel.Dispatcher.ChannelHandler.DispatchAndReleasePump(RequestContext request, Boolean cleanThread, OperationContext currentOperationContext)    
in System.ServiceModel.Dispatcher.ChannelHandler.HandleRequest(RequestContext request, OperationContext currentOperationContext)    
in System.ServiceModel.Dispatcher.ChannelHandler.AsyncMessagePump(IAsyncResult result)    
in System.ServiceModel.Dispatcher.ChannelHandler.OnAsyncReceiveComplete(IAsyncResult result)    
in System.Runtime.Fx.AsyncThunk.UnhandledExceptionFrame(IAsyncResult result)    
in System.Runtime.AsyncResult.Complete(Boolean completedSynchronously)    
in System.Runtime.InputQueue1.AsyncQueueReader.Set(Item item)    
in System.Runtime.InputQueue1.EnqueueAndDispatch(Item item, Boolean canDispatchOnThisThread)    
in System.Runtime.InputQueue1.EnqueueAndDispatch(T item, Action dequeuedCallback, Boolean canDispatchOnThisThread)    
in System.ServiceModel.Channels.SingletonChannelAcceptor3.Enqueue(QueueItemType item, Action dequeuedCallback, Boolean canDispatchOnThisThread)    
in System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.CompleteParseAndEnqueue(IAsyncResult result)    
in System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.HandleParseIncomingMessage(IAsyncResult result)    
in System.Runtime.AsyncResult.SyncContinue(IAsyncResult result)    
in System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.BeginProcessInboundRequest(ReplyChannelAcceptor replyChannelAcceptor, Action dequeuedCallback, AsyncCallback callback, Object state)    
in System.ServiceModel.Channels.HttpChannelListener1.HttpContextReceivedAsyncResult1.ProcessHttpContextAsync()    
in System.ServiceModel.Channels.HttpChannelListener1.BeginHttpContextReceived(HttpRequestContext context, Action acceptorCallback, AsyncCallback callback, Object state)    
in System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)    
in System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()    
in System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()    
in System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)    
in System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)    
in System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)    
in System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped nativeOverlapped)    
in System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped nativeOverlapped)    
in System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)  

I read lots of documents about SP limitations bud didn't found any point about limit of conditions count.

Please help

UPDATE: URL length is not a problem - the same issue can be with much more longer url (like VeryLongFiedName eq 123456 or VeryLongFiedName eq 123456 ...). If I have 159 conditions like VeryLongFiedName eq 123456 joined by or it works, but if there are 160 conditions it returns the error

Was it helpful?

Solution

Generally you shouldn't query with so many conditions unless it is really necessary. Following is a workaround which you can try.

If there are limitations in REST api multiple query conditions then you might think of using caml query IN operator in REST API.

function restCallwithCaml(listName, caml) {  
    /// get the site url  
    var siteUrl = _spPageContextInfo.siteAbsoluteUrl;  
    /// set request data  
    var data = { "query" :{"__metadata": { "type": "SP.CamlQuery" }, "ViewXml": caml}};  
    /// make an ajax call  
    $.ajax({  
       url: siteUrl+"/_api/web/lists/GetByTitle('"+ listName +"')/GetItems",  
            method: "POST",  
            data: data,  
            headers: {  
               "X-RequestDigest": $("#__REQUESTDIGEST").val(),  
               'content-type': 'application/json;odata=verbose',  
               'accept': 'application/json;odata=verbose'  
            }  
            success: function (response) {  
            ///do your code  
            },  
            error: function (data) {  
            ///do your code  
           }  
    });  
}

You can define caml query like this to have more than 500 conditions.

<View>
    <ViewFields>
        <FieldRef Name='ID'/>
        <FieldRef Name='Title'/>
    </ViewFields>
    <Query>
        <Where>
            <Or>
                <In>
                    <FieldRef Name='ID' />
                    <Values>
                        <Value Type='Integer'>1</Value>
                        <Value Type='Integer'>2</Value>
                        <Value Type='Integer'>3</Value>
                        ...
                        <Value Type='Integer'>499</Value>
                    </Values>
                 </In>
                <In>
                    <FieldRef Name='ID' />
                    <Values>
                        <Value Type='Integer'>500</Value>
                        <Value Type='Integer'>501</Value>
                        <Value Type='Integer'>502</Value>
                        ...
                        <Value Type='Integer'>999</Value>
                    </Values>
                 </In>
            </Or>
        </Where>
    </Query>
</View>

Reference:
SharePoint REST API - CAML Query
caml-query-limitation-of-values-in-in-operator

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top