Skip to content

Commit

Permalink
SharePoint API: support for multi lookup field value
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Feb 5, 2022
1 parent 10d5746 commit 04a2b2c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
8 changes: 7 additions & 1 deletion examples/SharePoint/ListItems/SetLookupFieldValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\FieldLookupValue;
use Office365\SharePoint\FieldMultiLookupValue;
use Office365\SharePoint\FieldUserValue;
use Office365\SharePoint\ListItem;

$credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
Expand All @@ -20,8 +22,12 @@
return;
}
$taskId = $items[0]->getProperty("Id");
$me = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();

$taskProps = array(
'Title' => "New task N#" . rand(1, 100000),
'ParentTask' => new FieldLookupValue($taskId)
'ParentTask' => new FieldLookupValue($taskId),
'PrimaryManager' => new FieldUserValue($me->getId()),
'Managers' => new FieldMultiLookupValue([$me->getId()])
);
$item = $list->addItem($taskProps)->executeQuery();
4 changes: 3 additions & 1 deletion src/SharePoint/FieldLookupValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public function __construct($LookupId)
*/
public $LookupValue;

}
}


26 changes: 26 additions & 0 deletions src/SharePoint/FieldMultiLookupValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Office365\SharePoint;

use Office365\Runtime\ClientValueCollection;

class FieldMultiLookupValue extends ClientValueCollection
{
public function __construct($lookupIds)
{
parent::__construct(FieldLookupValue::class);
foreach ($lookupIds as $lookupId) {
$this->addChild(new FieldLookupValue($lookupId));
}
}


public function toJson()
{
$lookupIds = array_map(function (FieldLookupValue $value) {
return $value->LookupId;
}, $this->getData());
return array('results' => $lookupIds);
}

}
15 changes: 15 additions & 0 deletions src/SharePoint/FieldUserValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,20 @@

class FieldUserValue extends FieldLookupValue
{

/**
* Initialize field value from User
* @param User $user
* @return FieldUserValue
*/
public static function fromUser($user){
$value = new FieldUserValue(-1);
$user->ensureProperty("Id",function () use($value, $user){
$value->LookupId = $user->getId();
});
return $value;
}


public $Email;
}
12 changes: 9 additions & 3 deletions src/SharePoint/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,12 +430,18 @@ public function getServerTypeName()

public function setProperty($name, $value, $persistChanges = true)
{
if($value instanceof FieldLookupValue){
if ($value instanceof FieldMultiLookupValue){
parent::setProperty("{$name}Id", $value, true);
parent::setProperty($name, $value, false);
}
elseif($value instanceof FieldLookupValue){
parent::setProperty("{$name}Id", $value->LookupId, true);
parent::setProperty($name, $value, false);
return $this;
}
return parent::setProperty($name, $value, $persistChanges);
else{
parent::setProperty($name, $value, $persistChanges);
}
return $this;
}

/**
Expand Down

0 comments on commit 04a2b2c

Please sign in to comment.