phpSPO icon indicating copy to clipboard operation
phpSPO copied to clipboard

GetItems with CamlQuery

Open tomeucapo opened this issue 5 years ago • 9 comments

Hi

I tried to load elements of library using CAML Query and exception occurred. I'm Using 2.4.1 version and PHP 7.2. My code is:


$Url = "https://AAAAAA.sharepoint.com/sites/AAAAAA";

$authCtx = new AuthenticationContext($Url);
$authCtx->acquireTokenForUser("[email protected]","AAAA");

$ctx = new ClientContext($Url,$authCtx); //initialize REST client
$list = $ctx->getWeb()->getLists()->getByTitle("Documents Venta");

$camlQuery = new CamlQuery();
$camlQuery->ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='Tipus' /><Value Type='Number'>32</Value></Eq><And><Geq><FieldRef Name='Created' /><Value Type='Datetime' IncludeTimeValue='True'>2020-07-06T17:24:11Z</Value></Geq><Leq><FieldRef Name='Created' /><Value Type='Datetime' IncludeTimeValue='True'>2020-07-31T17:24:11Z</Value></Leq></And></And></Where></Query></View>";

$items = $list->getItems($camlQuery);
$ctx->load($items);
$ctx->executeQuery();

foreach($items as $item)
{
        echo "{$item->Num_Registre} {$item->Created}\n";
}

And programs throws an exception:

PHP Fatal error: Uncaught Error: Call to a member function getSegment() on null in /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/ClientObject.php:235 Stack trace: #0 /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/ClientObjectCollection.php(263): Office365\Runtime\ClientObject->setProperty('Id', 6851, false) #1 /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/OData/ODataRequest.php(183): Office365\Runtime\ClientObjectCollection->setProperty(0, Array, false) #2 /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/OData/ODataRequest.php(159): Office365\Runtime\OData\ODataRequest->mapJson(Array, Object(Office365\SharePoint\ListItemCollection), Object(Office365\Runtime\OData\JsonLightFormat)) #3 /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/ClientRequest.php(116): Office365\Runtime\OData\ODataRequest->processResponse(Object(Office365\Runtime\Http\Response)) #4 /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/ClientRuntimeContext.php(126): Office365\Runtime\ClientRequest->executeQuery() in /var/www/apps/Personal/vendor/vgrem/php-spo/src/Runtime/ClientObject.php on line 235

I do something wrong or library can't process response?

I try to do this with C# Microsoft Sharepoint library and works:

            List list = clientContext.Web.Lists.GetByTitle("Documents Venta");
            
            CamlQuery camlQuery = new CamlQuery();
           
            camlQuery.ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='Tipus' /><Value Type='Number'>32</Value></Eq><And><Geq><FieldRef Name='Created' /><Value Type='Datetime' IncludeTimeValue='True'>2020-07-06T17:24:11Z</Value></Geq><Leq><FieldRef Name='Created' /><Value Type='Datetime' IncludeTimeValue='True'>2020-07-31T17:24:11Z</Value></Leq></And></And></Where></Query></View>";
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            var i = 0;
            foreach (ListItem listItem in listItems)
            {
                Console.WriteLine(listItem["Num_Registre"].ToString()+" "+listItem["Fecha_Documento"].ToString() + " " + listItem["Created"].ToString());
                i++;
            }

            Console.WriteLine($"Total items = {i}");

I looked into issues and exists other same issue:

https://github.com/vgrem/phpSPO/issues/180

Bugfix is not available on 2.4.1?

Thanks for advance

tomeucapo avatar Jul 13 '20 18:07 tomeucapo

Greetings,

seems like the issue on library side. Could you update to the latest version (2.4.2 at moment) and give it another shot?

vgrem avatar Aug 24 '20 20:08 vgrem

Thanks

Try to update 2.4.2 stable version and try again.

tomeucapo avatar Sep 03 '20 16:09 tomeucapo

I too am having problems accessing items in a list with CamlQuery. It's as if Sharepoint is disregarding my CamlQuery

$invoiceCamlQuery->ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='custnumber' /><Value Type='Number'>1234567</Value></Eq><Eq><FieldRef Name='invoicenumber' /><Value Type='Number'>7654321</Value></Eq></And></Where></Query></View>";

the fields are correct and I can get the desired item from the query above using: $list->getItemById(0987654321);

When using the CamlQuery I am getting an error from: {"error":{"code":"-2147024860, Microsoft.SharePoint.SPQueryThrottledException","message":{"lang":"en-US","value":"The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator."}}}. I have been told this is a very large list, about 1.7M items.

I am third party dev and dont have direct access to the Sharepoint in question.

I am using v2.4.2 and php 7.2.22

wisnetmark avatar Sep 10 '20 21:09 wisnetmark

I too am having problems accessing items in a list with CamlQuery. It's as if Sharepoint is disregarding my CamlQuery

$invoiceCamlQuery->ViewXml = "<View><Query><Where><And><Eq><FieldRef Name='custnumber' /><Value Type='Number'>1234567</Value></Eq><Eq><FieldRef Name='invoicenumber' /><Value Type='Number'>7654321</Value></Eq></And></Where></Query></View>";

the fields are correct and I can get the desired item from the query above using: $list->getItemById(0987654321);

When using the CamlQuery I am getting an error from: {"error":{"code":"-2147024860, Microsoft.SharePoint.SPQueryThrottledException","message":{"lang":"en-US","value":"The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator."}}}. I have been told this is a very large list, about 1.7M items.

I am third party dev and dont have direct access to the Sharepoint in question.

I am using v2.4.2 and php 7.2.22

Hello appears your CamlQuery not work proppertly and list all items from library and throws this error. If can you test CamlQuery before used it into vgrem library can certify if expression is correct or not, For example I try my CamlQuery with .net Microsoft Sharepoint library to certify this, or can you use U2U CAML Query Builder found at https://www.u2u.be/software.

Thanks

tomeucapo avatar Sep 13 '20 09:09 tomeucapo

i got this working. the CamlQuery was indeed built with a builder and provided to me via my client. it worked for them in their own production environment. The QueryList.php example in the library had one piece of syntax that was not given to me. it was the LookupId="TRUE". including that allowed my query to work. ZERO clue why that piece would be important on a non "id" field

wisnetmark avatar Sep 14 '20 14:09 wisnetmark

I am having an issue trying to get a single record back from a SharePoint list using CamlQuery.
Here is my code:

$xml = <View><Query><Where><Eq><FieldRef Name="Title" LookupId="TRUE"/><Value Type="Text">123456</Value></Eq></Where></Query></View>';

    $qry = new CamlQuery();
    $qry->ViewXml = $xml;
    $listItems = $spList->getItems($qry);
    $ctx->load($listItems);
    $ctx->executeQuery();
    print($listItems->getCount() .PHP_EOL);
    foreach($listItems as $index => $listItem)
    {
        print($index .":" .$listItem->getProperty('Title'). PHP_EOL);
    }

This is returning ALL of the records in the list instead of the one I want.

I am using version 2.4.3 of the library.

JerryGriffiths avatar Jan 15 '21 17:01 JerryGriffiths

I am having this same issue, trying to query for "Market" = "Orlando"

$qry = new CamlQuery();
$qry->ViewXml = '<View><Query><Where><Eq><FieldRef Name="Market" LookupId="TRUE"></FieldRef><Value Type="Text">Orlando</Value></Eq></Where></Query></View>';
$items = $list->getItems($qry);

//$items = $list->getItems();
$ctx->load($items);
$ctx->executeQuery();

foreach ($items as $index => $item){
    print($index . ":" . $item->getProperty('Market') . PHP_EOL);
    echo '<BR>';
}

Output:

0:Orlando 1:Dallas 2:Orlando 3:Orlando 4:Houston 5:Houston 6:Dallas 7:Atlanta 8:Dallas 9:Dallas

jasonalsing avatar Feb 05 '21 17:02 jasonalsing

I upgraded to the latest version (2.5.0) and I am still having this issue. Caml query are just ignored.

jasonalsing avatar May 21 '21 14:05 jasonalsing

I found similar issue mentioned in https://github.com/vgrem/phpSPO/issues/216 and the workaround is working for me.

Remove: $ctx->load($items);

Query returns the proper filtered data based on the $qry.

jasonalsing avatar Aug 25 '21 20:08 jasonalsing