From a3ffafa77ca6b75e62b25d143fd62f5f15f7e48b Mon Sep 17 00:00:00 2001 From: "vvgrem@gmail.com" Date: Sat, 15 May 2021 22:06:38 +0300 Subject: [PATCH] Teams API methods, prepare for 2.5.0 release --- README.md | 27 ++++++++++++- src/Common/Directory.php | 6 ++- src/Common/Group.php | 21 ++++++++-- src/SharePoint/Field.php | 3 -- src/SharePoint/ListItem.php | 3 -- .../UserProfiles/HashTagCollection.php | 22 +++++----- src/SharePoint/UserProfiles/UserProfile.php | 8 ++-- src/Teams/Team.php | 40 ++++++++++++++++++- tests/teams/TeamsTest.php | 38 +++++++++++++++++- 9 files changed, 136 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 22ffd94d..3138bfa8 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ $web = $client->getWeb(); $list = $web->getLists()->getByTitle("{list-title}"); //init List resource $items = $list->getItems(); //prepare a query to retrieve from the $client->load($items); //save a query to retrieve list items from the server -$client->executeQuery(); //submit query to SharePoint Online REST service +$client->executeQuery(); //submit query to SharePoint server /** @var ListItem $item */ foreach($items as $item) { print "Task: {$item->getProperty('Title')}\r\n"; @@ -198,6 +198,31 @@ $client->executeQuery(); ### Working with Teams API +#### Example: create a Team +The following is an example of a minimal request to create a Team (via delegated permissions) + +```php + +use Office365\GraphServiceClient; +use Office365\Runtime\Auth\AADTokenProvider; +use Office365\Runtime\Auth\UserCredentials; + +function acquireToken() +{ + $tenant = "{tenant}.onmicrosoft.com"; + $resource = "https://graph.microsoft.com"; + + $provider = new AADTokenProvider($tenant); + return $provider->acquireTokenForPassword($resource, "{clientId}", + new UserCredentials("{UserName}", "{Password}")); +} + +$client = new GraphServiceClient("acquireToken"); +$teamName = "My Sample Team"; +$newTeam = $client->getTeams()->add($teamName)->executeQuery(); + +``` + ### Working with Outlook API diff --git a/src/Common/Directory.php b/src/Common/Directory.php index 86f4fc39..058f16ea 100644 --- a/src/Common/Directory.php +++ b/src/Common/Directory.php @@ -5,11 +5,13 @@ */ namespace Office365\Common; -use Office365\Runtime\ClientObject; +use Office365\Entity; /** * ". Deleted items will remain available to restore for up to 30 days. After 30 days, the items are permanently deleted." */ -class Directory extends ClientObject +class Directory extends Entity { + + } \ No newline at end of file diff --git a/src/Common/Group.php b/src/Common/Group.php index 241b0738..596d8240 100644 --- a/src/Common/Group.php +++ b/src/Common/Group.php @@ -20,6 +20,15 @@ */ class Group extends DirectoryObject { + + /** + * Create a new team under a group. + */ + public function addTeam(){ + + } + + /** * Describes a classification for the group (such as low, medium or high business impact). Valid values for this property are defined by creating a ClassificationList [setting](groupsetting.md) value, based on the [template definition](groupsettingtemplate.md).

Returned by default. * @return string @@ -44,13 +53,16 @@ public function getDescription() { return $this->getProperty("Description"); } + /** * An optional description for the group.

Returned by default. + * + * @return self * @var string */ public function setDescription($value) { - $this->setProperty("Description", $value, true); + return $this->setProperty("Description", $value, true); } /** * The display name for the group. This property is required when a group is created and cannot be cleared during updates.

Returned by default. Supports $filter and $orderby. @@ -60,13 +72,16 @@ public function getDisplayName() { return $this->getProperty("DisplayName"); } + /** - * The display name for the group. This property is required when a group is created and cannot be cleared during updates.

Returned by default. Supports $filter and $orderby. + * The display name for the group. This property is required when a group is created and cannot be cleared during updates.

Returned by default. Supports $filter and $orderby. + * + * @return self * @var string */ public function setDisplayName($value) { - $this->setProperty("DisplayName", $value, true); + return $this->setProperty("DisplayName", $value, true); } /** * Indicates whether there are members in this group that have license errors from its group-based license assignment.

This property is never returned on a GET operation. You can use it as a $filter argument to get groups that have members with license errors (that is, filter for this property being true). See an [example](../api/group-list.md). diff --git a/src/SharePoint/Field.php b/src/SharePoint/Field.php index ac52e6d6..ec1bd8ed 100644 --- a/src/SharePoint/Field.php +++ b/src/SharePoint/Field.php @@ -874,9 +874,6 @@ public function setValidationFormula($value) */ public function getValidationMessage() { - if (!$this->isPropertyAvailable("ValidationMessage")) { - return null; - } return $this->getProperty("ValidationMessage"); } /** diff --git a/src/SharePoint/ListItem.php b/src/SharePoint/ListItem.php index 5c69411f..ac3629c5 100644 --- a/src/SharePoint/ListItem.php +++ b/src/SharePoint/ListItem.php @@ -299,9 +299,6 @@ public function setIconOverlay($value) */ public function getId() { - if (!$this->isPropertyAvailable("Id")) { - return null; - } return $this->getProperty("Id"); } /** diff --git a/src/SharePoint/UserProfiles/HashTagCollection.php b/src/SharePoint/UserProfiles/HashTagCollection.php index dbf037c5..58347b10 100644 --- a/src/SharePoint/UserProfiles/HashTagCollection.php +++ b/src/SharePoint/UserProfiles/HashTagCollection.php @@ -5,26 +5,22 @@ */ namespace Office365\SharePoint\UserProfiles; -use Office365\Runtime\ClientObject; +use Office365\Runtime\ResourcePath; +use Office365\SharePoint\BaseEntity; +use Office365\SharePoint\BaseEntityCollection; -class HashTagCollection extends ClientObject + +class HashTagCollection extends BaseEntity { /** * @return HashTagCollection */ public function getItems() { - if (!$this->isPropertyAvailable("Items")) { - return null; - } - return $this->getProperty("Items"); - } - /** - * @var HashTagCollection - */ - public function setItems($value) - { - $this->setProperty("Items", $value, true); + return $this->getProperty("Items", + new BaseEntityCollection($this->getContext(), + new ResourcePath("items",$this->resourcePath), HashTag::class)); } + } \ No newline at end of file diff --git a/src/SharePoint/UserProfiles/UserProfile.php b/src/SharePoint/UserProfiles/UserProfile.php index f82f813b..8b985987 100644 --- a/src/SharePoint/UserProfiles/UserProfile.php +++ b/src/SharePoint/UserProfiles/UserProfile.php @@ -10,10 +10,7 @@ class UserProfile extends BaseEntity { - /*public function __construct(ClientRuntimeContext $ctx) - { - parent::__construct($ctx, new ResourcePath("SP.UserProfiles.ProfileLoader.getProfileLoader/getUserProfile")); - }*/ + /** * Enqueues creating a personal site for this user, which can be used to share documents, web pages, and other files. */ @@ -21,6 +18,7 @@ public function createPersonalSiteEnque() { $qry = new InvokePostMethodQuery($this, "createPersonalSiteEnque", array(false)); $this->getContext()->addQuery($qry); + return $this; } /** * @return string @@ -396,7 +394,7 @@ public function getUrlToCreatePersonalSite() /** * @param $value - * @return $this + * @return self */ public function setUrlToCreatePersonalSite($value) { diff --git a/src/Teams/Team.php b/src/Teams/Team.php index f455a976..89ef8459 100644 --- a/src/Teams/Team.php +++ b/src/Teams/Team.php @@ -5,8 +5,13 @@ */ namespace Office365\Teams; +use Office365\Common\DirectoryObject; +use Office365\Common\User; use Office365\Entity; +use Office365\EntityCollection; +use Office365\Runtime\Actions\InvokePostMethodQuery; use Office365\Runtime\Http\RequestOptions; +use Office365\Runtime\ResourcePath; /** @@ -129,9 +134,42 @@ public function setFunSettings($value) return $this->setProperty("FunSettings", $value, true); } + /** + * @return EntityCollection + */ + public function getMembers() + { + return $this->getProperty("Members", + new EntityCollection($this->getContext(), + new ResourcePath("Members",$this->getResourcePath()),DirectoryObject::class)); + } + + + /** + * @param User $user + * @param string[] $roles + * @return Entity + */ + public function addMember($user, $roles){ + $returnType = new Entity($this->getContext()); + $this->getMembers()->addChild($returnType); + + $user->ensureProperty("Id",function () use ($user, $returnType, $roles){ + $payload = array( + "@odata.type" => "#microsoft.graph.aadUserConversationMember", + "roles" => $roles, + "user@odata.bind" => "https://graph.microsoft.com/v1.0/users('{$user->getId()}')" + ); + $qry = new InvokePostMethodQuery($this->getMembers(),null,null,null,$payload); + $this->getContext()->addQueryAndResultObject($qry, $returnType); + + }); + return $returnType; + } + /** * Deletes a Team - * @return Team + * @return self */ public function deleteObject() { diff --git a/tests/teams/TeamsTest.php b/tests/teams/TeamsTest.php index 6d4723da..92c066f0 100644 --- a/tests/teams/TeamsTest.php +++ b/tests/teams/TeamsTest.php @@ -3,6 +3,7 @@ namespace Office365; +use Office365\Common\User; use Office365\Runtime\Http\RequestException; use Office365\Teams\Team; use Office365\Teams\TeamGuestSettings; @@ -58,6 +59,41 @@ public function testUpdateTeam(Team $team) self::assertNotNull($team->getResourcePath()); } + /** + * @depends testGetTeam + * @param Team $team + * @throws \Exception + */ + public function testAddTeamMember(Team $team) + { + /** @var User $user */ + $user = self::$graphClient->getUsers()->getById(self::$testAccountName); + $member = $team->addMember($user, ["owner"])->executeQuery(); + self::assertNotNull($member->getResourcePath()); + return $member; + } + + /** + * @depends testGetTeam + * @param Team $team + * @throws \Exception + */ + public function testListTeamMembers(Team $team) + { + $members = $team->getMembers()->get()->executeQueryRetry(3,5); + self::assertNotNull($members->getResourcePath()); + } + + /** + * @depends testAddTeamMember + * @param Entity $member + * @throws \Exception + */ + public function testRemoveTeamMember(Entity $member) + { + $member->deleteObject()->executeQuery(); + } + /** * @depends testGetTeam * @param Team $team @@ -66,7 +102,7 @@ public function testUpdateTeam(Team $team) public function testDeleteTeam(Team $team) { $deletedId = $team->getId(); - $team->deleteObject()->executeQuery(); + $team->deleteObject()->executeQueryRetry(); try { self::$graphClient->getGroups()->getById($deletedId)->get()->executeQuery(); }