diff --git a/examples/SharePoint/ListItems/SetLookupFieldValue.php b/examples/SharePoint/ListItems/SetLookupFieldValue.php index f5039d45..195a5b3c 100644 --- a/examples/SharePoint/ListItems/SetLookupFieldValue.php +++ b/examples/SharePoint/ListItems/SetLookupFieldValue.php @@ -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']); @@ -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(); diff --git a/src/SharePoint/FieldLookupValue.php b/src/SharePoint/FieldLookupValue.php index 5cb12223..4c6dc06c 100644 --- a/src/SharePoint/FieldLookupValue.php +++ b/src/SharePoint/FieldLookupValue.php @@ -29,4 +29,6 @@ public function __construct($LookupId) */ public $LookupValue; -} \ No newline at end of file +} + + diff --git a/src/SharePoint/FieldMultiLookupValue.php b/src/SharePoint/FieldMultiLookupValue.php new file mode 100644 index 00000000..80380b52 --- /dev/null +++ b/src/SharePoint/FieldMultiLookupValue.php @@ -0,0 +1,26 @@ +addChild(new FieldLookupValue($lookupId)); + } + } + + + public function toJson() + { + $lookupIds = array_map(function (FieldLookupValue $value) { + return $value->LookupId; + }, $this->getData()); + return array('results' => $lookupIds); + } + +} \ No newline at end of file diff --git a/src/SharePoint/FieldUserValue.php b/src/SharePoint/FieldUserValue.php index 0711a6e0..a8aa4712 100644 --- a/src/SharePoint/FieldUserValue.php +++ b/src/SharePoint/FieldUserValue.php @@ -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; } \ No newline at end of file diff --git a/src/SharePoint/ListItem.php b/src/SharePoint/ListItem.php index 6cdbd1cf..2d9cf95f 100644 --- a/src/SharePoint/ListItem.php +++ b/src/SharePoint/ListItem.php @@ -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; } /**