shareplum icon indicating copy to clipboard operation
shareplum copied to clipboard

'And' condition don't work correctly

Open aeperfilev opened this issue 7 years ago • 6 comments

Where rule didn't work correctly for more then 2 conditions.

If I use few conditions to get list elements, like this:

    Where: [
        ('Eq', Field1', 'Value1'),
        ('Eq', Field2', 'Value2'),
        ('Eq', Field3', 'Value3'),
    ]

then SharPlum will return elements for 1st condition, other conditions will be ignored.

Same result I will get if I use 'And' condition, like this:

    Where: [
        ('Eq', Field1', 'Value1'),
        'And',
        ('Eq', Field2', 'Value2'),
        'And',
        ('Eq', Field3', 'Value3'),
    ]

But if I add 'And' mark like this:

    Where: [
        'And',
        ('Eq', Field1', 'Value1'),
        ('Eq', Field2', 'Value2'),
        ('Eq', Field3', 'Value3'),
    ]

I will get error response from SOAP:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Server</faultcode>
      <faultstring>Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.</faultstring>
      <detail>
        <errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">Cannot Complete this action</errorstring>
        <errorcode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x80004005</errorcode>
      </detail>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

For two conditions it seems work fine...

aeperfilev avatar Nov 08 '17 10:11 aeperfilev

Seems, there are common limitations on queries:

CAML query can contains at most two comparison statement inside <OR> or <AND> block.

From: https://social.msdn.microsoft.com/Forums/office/en-US/f615e8df-8206-46d1-8a0f-3a3fad47bf43/caml-query-with-more-than-two-or-statements?forum=sharepointdevelopmentlegacy

aeperfilev avatar Nov 08 '17 13:11 aeperfilev

So. after "googling" the solution looks like include multiple conditions in top-level condition, like this (for 3 conditions):

    Where: [
        'And',
        ('Eq', Field1', 'Value1'),
        'And',
        ('Eq', Field2', 'Value2'),
        ('Eq', Field3', 'Value3'),
    ]

For more conditions it would be like this:

    Where: [
        'And',
        ('Eq', Field1', 'Value1'),
        'And',
        ('Eq', Field2', 'Value2'),
        'And',
        ('Eq', Field3', 'Value3'),
        'And',
        ('Eq', Field4', 'Value4'),
        'And',
        .......
        'And',
        ('Eq', FieldM', 'ValueM'),
        ('Eq', FieldN', 'ValueN'),
    ]

May be it would be good to process this on SharePlum side?

Also, there is no ability to close conditions like 'And' and 'Or'. So if I want to make more comlicate condition, it would be problematic.

aeperfilev avatar Nov 08 '17 13:11 aeperfilev

Interesting. Top notch work on this. Do you have any suggestions on improving the syntax for these queries?

jasonrollins avatar Nov 10 '17 03:11 jasonrollins

Do you have any suggestions on improving the syntax for these queries?

Query syntax may be like this:

query = {
    'Where': [
        ('And', [
            ('Eq', 'Field1', 'Value1'),
            ('Eq', 'Field2', 'Value2'),
            ('Eq', 'Field3', 'Value3'),
        ]),
        ('Eq', 'Field4', 'Value4'),
    ]
    'OrderBy': ['Field1']
}

And I think that your script must provide normal work of incorrect number of conditions by itself, in another words control number of conditions in 'AND'-block and break them into nested conditions, e.g.:

query = {
    'Where': [
        ('And', [
            ('Eq', 'Field1', 'Value1'),
            ('And', [
                ('Eq', 'Field2', 'Value2'),
                ('Eq', 'Field3', 'Value3'),
            ])
        ]),
        ('Eq', 'Field4', 'Value4'),
    ]
    'OrderBy': ['Field1']
}

But anyway it's not transparent how first condition (AND-block) related to second condition for field4.

aeperfilev avatar Nov 10 '17 10:11 aeperfilev

+1 ran into this problem

gwsampso avatar Feb 19 '19 03:02 gwsampso

@gwsampso Can you describe the query you are trying to run?

jasonrollins avatar Feb 20 '19 02:02 jasonrollins