Skip to content

Commit 04a2b2c

Browse files
committed
SharePoint API: support for multi lookup field value
1 parent 10d5746 commit 04a2b2c

File tree

5 files changed

+60
-5
lines changed

5 files changed

+60
-5
lines changed

examples/SharePoint/ListItems/SetLookupFieldValue.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Office365\Runtime\Auth\ClientCredential;
88
use Office365\SharePoint\ClientContext;
99
use Office365\SharePoint\FieldLookupValue;
10+
use Office365\SharePoint\FieldMultiLookupValue;
11+
use Office365\SharePoint\FieldUserValue;
1012
use Office365\SharePoint\ListItem;
1113

1214
$credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
@@ -20,8 +22,12 @@
2022
return;
2123
}
2224
$taskId = $items[0]->getProperty("Id");
25+
$me = $ctx->getWeb()->getCurrentUser()->get()->executeQuery();
26+
2327
$taskProps = array(
2428
'Title' => "New task N#" . rand(1, 100000),
25-
'ParentTask' => new FieldLookupValue($taskId)
29+
'ParentTask' => new FieldLookupValue($taskId),
30+
'PrimaryManager' => new FieldUserValue($me->getId()),
31+
'Managers' => new FieldMultiLookupValue([$me->getId()])
2632
);
2733
$item = $list->addItem($taskProps)->executeQuery();

src/SharePoint/FieldLookupValue.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ public function __construct($LookupId)
2929
*/
3030
public $LookupValue;
3131

32-
}
32+
}
33+
34+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Office365\SharePoint;
4+
5+
use Office365\Runtime\ClientValueCollection;
6+
7+
class FieldMultiLookupValue extends ClientValueCollection
8+
{
9+
public function __construct($lookupIds)
10+
{
11+
parent::__construct(FieldLookupValue::class);
12+
foreach ($lookupIds as $lookupId) {
13+
$this->addChild(new FieldLookupValue($lookupId));
14+
}
15+
}
16+
17+
18+
public function toJson()
19+
{
20+
$lookupIds = array_map(function (FieldLookupValue $value) {
21+
return $value->LookupId;
22+
}, $this->getData());
23+
return array('results' => $lookupIds);
24+
}
25+
26+
}

src/SharePoint/FieldUserValue.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,20 @@
1010

1111
class FieldUserValue extends FieldLookupValue
1212
{
13+
14+
/**
15+
* Initialize field value from User
16+
* @param User $user
17+
* @return FieldUserValue
18+
*/
19+
public static function fromUser($user){
20+
$value = new FieldUserValue(-1);
21+
$user->ensureProperty("Id",function () use($value, $user){
22+
$value->LookupId = $user->getId();
23+
});
24+
return $value;
25+
}
26+
27+
1328
public $Email;
1429
}

src/SharePoint/ListItem.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,12 +430,18 @@ public function getServerTypeName()
430430

431431
public function setProperty($name, $value, $persistChanges = true)
432432
{
433-
if($value instanceof FieldLookupValue){
433+
if ($value instanceof FieldMultiLookupValue){
434+
parent::setProperty("{$name}Id", $value, true);
435+
parent::setProperty($name, $value, false);
436+
}
437+
elseif($value instanceof FieldLookupValue){
434438
parent::setProperty("{$name}Id", $value->LookupId, true);
435439
parent::setProperty($name, $value, false);
436-
return $this;
437440
}
438-
return parent::setProperty($name, $value, $persistChanges);
441+
else{
442+
parent::setProperty($name, $value, $persistChanges);
443+
}
444+
return $this;
439445
}
440446

441447
/**

0 commit comments

Comments
 (0)