Skip to content

Commit

Permalink
Introduced support for skiptoken operation in query options #118
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Oct 27, 2018
1 parent 4e2bcbe commit 493b94b
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 47 deletions.
5 changes: 2 additions & 3 deletions examples/SharePoint/view_examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_once('../bootstrap.php');

use Office365\PHP\Client\Runtime\Auth\AuthenticationContext;
use Office365\PHP\Client\Runtime\ClientAction;
use Office365\PHP\Client\Runtime\Utilities\RequestOptions;
use Office365\PHP\Client\SharePoint\ClientContext;

global $Settings;
Expand All @@ -12,18 +14,15 @@
$authCtx = new AuthenticationContext($Settings['Url']);
$authCtx->acquireTokenForUser($Settings['UserName'],$Settings['Password']);
$ctx = new ClientContext($Settings['Url'],$authCtx);

//$listTitle = "Orders_" . rand(1,1000);
$listTitle = "Tasks" ;

printListViews($ctx,$listTitle);
}
catch (Exception $e) {
echo 'Error: ', $e->getMessage(), "\n";
}



function printListViews(ClientContext $ctx, $listTitle){
$list = $ctx->getWeb()->getLists()->getByTitle($listTitle);
$views = $list->getViews();
Expand Down
12 changes: 12 additions & 0 deletions src/Runtime/ClientObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ public function skip($value)
}


/**
* Sets the number of records to skip before it retrieves records in a collection.
* @param $value
* @return ClientObjectCollection $this
*/
public function skiptoken($value)
{
$this->queryOptions->SkipToken = rawurlencode($value);
return $this;
}


/**
* Creates resource for a collection
* @return ClientObject
Expand Down
2 changes: 2 additions & 0 deletions src/Runtime/OData/ODataQueryOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ private function getProperties(){

public $Skip;

public $SkipToken;

public $Search;
}
45 changes: 24 additions & 21 deletions src/Runtime/OData/ODataRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Exception;
use Office365\PHP\Client\Runtime\ClientAction;
use Office365\PHP\Client\Runtime\ClientObject;
use Office365\PHP\Client\Runtime\ClientRequestStatus;
use Office365\PHP\Client\Runtime\ClientResult;
use Office365\PHP\Client\Runtime\IEntityType;
Expand Down Expand Up @@ -70,45 +71,44 @@ public function processResponse($response)
$resultObject = $this->resultObjects[$this->getCurrentAction()->getId()];

if ($this->getCurrentAction() instanceof InvokePostMethodQuery && $this->getCurrentAction()->MethodBody instanceof ChangeLogItemQuery) {
$payload = $this->parseXmlResponse($response);
$this->processXmlResponse($response,$resultObject);
} else {
$payload = $this->parseJsonResponse($response);
}

if ($resultObject instanceof ClientResult) {
if ($this->getCurrentAction() instanceof InvokeMethodQuery){
$this->getSerializationContext()->RootElement = $this->getCurrentAction()->MethodName;
}
$resultObject->fromJson($payload,$this->getSerializationContext());
} else if($resultObject instanceof IEntityType) {
$this->getSerializationContext()->map($payload,$resultObject);
$this->getCurrentAction()->getResourcePath()->ServerObjectIsNull = false;
$this->processJsonResponse($response,$resultObject);
}
}


/**
* @param string $response
* @return mixed
* @param ClientObject|ClientResult $resultObject
* @throws Exception
*/
private function parseJsonResponse($response)
private function processJsonResponse($response, $resultObject)
{
$error = array();
$errorPayload = array();
$payload = json_decode($response);
if ($this->validateResponse($payload, $error) == false) {
throw new Exception($error['Message']);
if ($this->validateResponse($payload, $errorPayload) == false) {
throw new Exception($errorPayload['Message']);
}

if ($resultObject instanceof ClientResult) {
if ($this->getCurrentAction() instanceof InvokeMethodQuery){
$this->getSerializationContext()->RootElement = $this->getCurrentAction()->MethodName;
}
$resultObject->fromJson($payload,$this->getSerializationContext());
} else if($resultObject instanceof IEntityType) {
$this->getSerializationContext()->map($payload,$resultObject);
$this->getCurrentAction()->getResourcePath()->ServerObjectIsNull = false;
}
return $payload;
}


/**
* Process Xml response from SharePoint REST service
* @param string $response
* @return array
* @param ClientObject $resultObject
*/
private function parseXmlResponse($response)
private function processXmlResponse($response, $resultObject)
{
$payload = array();
$xml = simplexml_load_string($response);
Expand All @@ -122,7 +122,7 @@ private function parseXmlResponse($response)
}
$payload[] = $item;
}
return $payload;
$this->getSerializationContext()->map($payload,$resultObject);
}


Expand Down Expand Up @@ -200,6 +200,9 @@ protected function getCurrentAction(){
}


/**
* @return ClientRequest
*/
public function getNextRequest()
{
$request = new ODataRequest($this->context);
Expand Down
21 changes: 8 additions & 13 deletions src/Runtime/Utilities/RequestOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,12 @@ public function __construct($url, $headers = array(), $data = null, $methodType
$this->Verbose = false;
$this->SSLVersion = null;
$this->StreamHandle = null;
$this->Data = $data;
}

public function toArray()
{
return [
'Url' => $this->Url,
'Method' => $this->Method,
'Headers' => $this->Headers,
'Data' => $this->Data,
'IncludeBody' => $this->IncludeBody,
'IncludeHeaders' => $this->IncludeHeaders,
'AuthType' => $this->AuthType,
'Verbose' => $this->Verbose,
'UserCredentials' => $this->UserCredentials,
'SSLVersion' => $this->SSLVersion,
'StreamHandle' => $this->StreamHandle,
];
return get_object_vars($this);
}

public function addCustomHeader($name, $value)
Expand Down Expand Up @@ -136,4 +125,10 @@ function ($k, $v) {
*/
public $StreamHandle;


/**
* @var string
*/
public $Proxy;

}
1 change: 0 additions & 1 deletion src/SharePoint/SPList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class SPList extends SecurableObject
*/
public function addItem(array $listItemCreationInformation)
{

$items = new ListItemCollection($this->getContext(),
new ResourcePathEntity($this->getContext(),$this->getResourcePath(),"items"));
$listItem = new ListItem($this->getContext());
Expand Down
5 changes: 3 additions & 2 deletions tests/ClientContextTests.php → tests/ClientContextTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?php


class ClientContextTests extends SharePointTestCase
class ClientContextTest extends SharePointTestCase
{

public function testIfSingleRequestProcessed()
{
try{
Expand Down Expand Up @@ -34,4 +35,4 @@ public function testIfMultipleRequestsProcessed()
}
}

}
}
2 changes: 1 addition & 1 deletion tests/ListItemExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public static function deleteListItems(\Office365\PHP\Client\SharePoint\SPList $
}



/**
* Create list item operation
* @param \Office365\PHP\Client\SharePoint\SPList $list
* @param array $itemProperties
* @return \Office365\PHP\Client\SharePoint\ListItem
* @throws Exception
*/
public static function createListItem(\Office365\PHP\Client\SharePoint\SPList $list, array $itemProperties){
$ctx = $list->getContext();
Expand Down
53 changes: 47 additions & 6 deletions tests/ListItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public static function tearDownAfterClass()
}




public function testItemsCount()
{
$itemsCount = self::$targetList->getProperty("ItemCount");
Expand All @@ -46,17 +44,17 @@ public function testCreateListItems()
$currentUser = self::$context->getWeb()->getCurrentUser();
self::$context->load($currentUser);
self::$context->executeQuery();

$itemProperties = array(
'Title' => 'Order Approval' . rand(1, 1000),
'Body' => 'Please review a task',
'AssignedToId' => $currentUser->getProperty("Id"),
'PredecessorsId' => array( 'results' => array($currentUser->getProperty("Id")))
//'__metadata' => array('type' => 'SP.Data.TasksListItem')
);
$item = ListItemExtensions::createListItem(self::$targetList, $itemProperties);
$this->assertEquals($item->getProperty('Body'), $itemProperties['Body']);
return $item;
$items = $this->populateList($itemProperties,1);
$firstItem = $items[0];
$this->assertEquals($firstItem->getProperty('Body'), $itemProperties['Body']);
return $firstItem;
}


Expand Down Expand Up @@ -134,6 +132,38 @@ public function testQueryOptionsForMultiUserField()
}


public function testQueryOptionsSkipToken()
{
$minItemsCount = 10;
$maxItemId = null;
$itemsCount = self::$targetList->getProperty("ItemCount");
if ($itemsCount < $minItemsCount) {
$itemProperties = array(
'Title' => 'Order Approval' . rand(1, 1000),
'Body' => 'Please review a task'
);
$this->populateList($itemProperties, $minItemsCount - $itemsCount);
}

$items = self::$targetList->getItems();
self::$context->load($items);
self::$context->executeQuery();
$maxItemId = max(
array_map(function (ListItem $item) {
return $item->getProperty("Id");
}, $items->getData())
);


$items = self::$targetList->getItems()
->top($minItemsCount)
->skiptoken("Paged=TRUE&p_SortBehavior=0&p_ID=" . $maxItemId);
self::$context->load($items);
self::$context->executeQuery();
$this->assertEquals(0, $items->getCount());
}



public function testDeleteListItems()
{
Expand All @@ -151,5 +181,16 @@ public function testDeleteListItems()
$this->assertEquals($itemsCount, 0);
}


public function populateList($itemProperties,$itemsCount)
{
$items = [];
$idx = 0;
while($idx < $itemsCount){
$items[] = ListItemExtensions::createListItem(self::$targetList, $itemProperties);
$idx++;
}
return $items;
}

}

0 comments on commit 493b94b

Please sign in to comment.