Skip to content

Commit

Permalink
SharePoint API: support for setting taxonomy field values
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Apr 27, 2022
1 parent 95b7251 commit 8810e14
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 16 deletions.
4 changes: 3 additions & 1 deletion examples/SharePoint/Taxonomy/SetTaxFieldValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
use Office365\Runtime\Auth\ClientCredential;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\Taxonomy\TaxonomyFieldValue;
use Office365\SharePoint\Taxonomy\TaxonomyFieldValueCollection;

$credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
$siteUrl = $settings['TeamSiteUrl'];
$ctx = (new ClientContext($siteUrl))->withCredentials($credentials);
$tasksList = $ctx->getWeb()->getLists()->getByTitle("Tasks");

$taxFieldValue = new TaxonomyFieldValue("Sweden", "f9a6dae9-633c-474b-b35e-b235cf2b9e73");
$taxFieldValueAlt = new TaxonomyFieldValue("Germany", "04872f90-9990-4fe4-8cff-32cc8ca05417");

$taskProps = array(
'Title' => "New task N#" . rand(1, 100000),
"Country" => $taxFieldValue,
#"Countries" => new TaxonomyFieldValueCollection(array($taxFieldValue))
"Countries" => new TaxonomyFieldValueCollection(array($taxFieldValue, $taxFieldValueAlt))
);
$item = $tasksList->addItem($taskProps)->executeQuery();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Resource path for addressing a single entity
*/
class EntityResourcePath extends ResourcePath
class EntityPath extends ResourcePath
{

/**
Expand Down
4 changes: 2 additions & 2 deletions src/SharePoint/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Office365\Runtime\ClientObject;
use Office365\Runtime\ClientRuntimeContext;
use Office365\Runtime\OData\ODataQueryOptions;
use Office365\Runtime\Paths\EntityResourcePath;
use Office365\Runtime\Paths\EntityPath;
use Office365\Runtime\ResourcePath;

/**
Expand Down Expand Up @@ -48,7 +48,7 @@ public function setProperty($name, $value, $persistChanges = true)
//fallback: determine entity by Id
if ($name === "Id") {
if (is_null($this->getResourcePath())) {
$this->resourcePath = new EntityResourcePath($value, $this->getParentCollection()->getResourcePath());
$this->resourcePath = new EntityPath($value, $this->getParentCollection()->getResourcePath());
}
}
parent::setProperty($name, $value, $persistChanges);
Expand Down
4 changes: 2 additions & 2 deletions src/SharePoint/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/
namespace Office365\SharePoint;

use Office365\Runtime\Paths\EntityResourcePath;
use Office365\Runtime\Paths\EntityPath;
use Office365\Runtime\ResourcePath;
/**
* Specifies
Expand All @@ -23,7 +23,7 @@ function setProperty($name, $value, $persistChanges = true)
$value = $value['StringValue'];
} else {
if ($name == "StringId") {
$this->resourcePath = new EntityResourcePath($value, $this->getParentCollection()->getResourcePath());
$this->resourcePath = new EntityPath($value, $this->getParentCollection()->getResourcePath());
}
}
return parent::setProperty($name, $value, $persistChanges);
Expand Down
7 changes: 4 additions & 3 deletions src/SharePoint/ListItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
use Office365\Runtime\Actions\InvokePostMethodQuery;
use Office365\Runtime\ClientResult;
use Office365\Runtime\ClientValueCollection;
use Office365\Runtime\Paths\EntityResourcePath;
use Office365\Runtime\Paths\EntityPath;
use Office365\Runtime\ResourcePath;
use Office365\Runtime\ServerTypeInfo;
use Office365\SharePoint\Taxonomy\TaxonomyFieldValue;
use Office365\SharePoint\Taxonomy\TaxonomyFieldValueCollection;

/**
* Specifies
Expand Down Expand Up @@ -448,13 +449,13 @@ public function setProperty($name, $value, $persistChanges = true)
parent::setProperty("{$name}Id", $value->LookupId, true);
parent::setProperty($name, $value, false);
}
elseif($value instanceof TaxonomyFieldValue){
elseif($value instanceof TaxonomyFieldValue || $value instanceof TaxonomyFieldValueCollection){
$this->setTaxonomyFieldValue($name, $value);
}
else{
parent::setProperty($name, $value, $persistChanges);
}
if($name == "Id" && $this->resourcePath instanceof EntityResourcePath){
if($name == "Id" && $this->resourcePath instanceof EntityPath){
$this->resourcePath->setKey($value);
}
return $this;
Expand Down
5 changes: 2 additions & 3 deletions src/SharePoint/Publishing/VideoItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
*/
namespace Office365\SharePoint\Publishing;

use Office365\Runtime\Paths\EntityResourcePath;
use Office365\Runtime\ResourcePath;
use Office365\Runtime\Paths\EntityPath;
use Office365\Runtime\Http\HttpMethod;
use Office365\Runtime\Http\RequestOptions;
use Office365\Runtime\ServerTypeInfo;
Expand Down Expand Up @@ -38,7 +37,7 @@ function setProperty($name, $value, $persistChanges = true)
{
if ($name == "ID") {
if (is_null($this->getResourcePath())) {
$this->resourcePath = new EntityResourcePath($value, $this->getParentCollection()->getResourcePath());
$this->resourcePath = new EntityPath($value, $this->getParentCollection()->getResourcePath());
}
}
parent::setProperty($name, $value, $persistChanges);
Expand Down
4 changes: 2 additions & 2 deletions src/SharePoint/SPList.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Office365\Runtime\Actions\CreateEntityQuery;
use Office365\Runtime\Actions\InvokeMethodQuery;
use Office365\Runtime\Actions\InvokePostMethodQuery;
use Office365\Runtime\Paths\EntityResourcePath;
use Office365\Runtime\Paths\EntityPath;
use Office365\Runtime\Paths\ServiceOperationPath;
use Office365\Runtime\ResourcePath;
use Office365\Runtime\ServerTypeInfo;
Expand Down Expand Up @@ -36,7 +36,7 @@ class SPList extends SecurableObject
public function addItem(array $listItemCreationInformation)
{
$items = new ListItemCollection($this->getContext(), new ResourcePath("items", $this->getResourcePath()));
$listItem = new ListItem($this->getContext(), new EntityResourcePath(null,$items->getResourcePath()));
$listItem = new ListItem($this->getContext(), new EntityPath(null,$items->getResourcePath()));
$listItem->setProperty("ParentList", $this, false);
$items->addChild($listItem);
foreach ($listItemCreationInformation as $key => $value) {
Expand Down
30 changes: 30 additions & 0 deletions src/SharePoint/Taxonomy/TaxonomyFieldValueCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace Office365\SharePoint\Taxonomy;


use Office365\Runtime\ClientValueCollection;
use Office365\Runtime\ServerTypeInfo;

class TaxonomyFieldValueCollection extends ClientValueCollection
{

public function __construct($values)
{
parent::__construct(TaxonomyFieldValue::class);
foreach ($values as $value)
$this->addChild($value);
}

public function __toString()
{
return implode(';#', $this->getData());
}

public function getServerTypeInfo()
{
return null;
}

}
4 changes: 2 additions & 2 deletions tests/common/ResourcePathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Office365;

use Office365\Runtime\Paths\EntityResourcePath;
use Office365\Runtime\Paths\EntityPath;
use Office365\Runtime\Paths\ServiceOperationPath;
use Office365\Runtime\Paths\ResourcePathUrl;
use Office365\Runtime\ResourcePath;
Expand All @@ -19,7 +19,7 @@ function testCreateEntitySetPath(){
}

function testCreateSingleEntityPath(){
$path = new EntityResourcePath(1, new ResourcePath("SiteUsers",new ResourcePath("Web")));
$path = new EntityPath(1, new ResourcePath("SiteUsers",new ResourcePath("Web")));
self::assertEquals("/Web/SiteUsers(1)",$path->toUrl());
}

Expand Down

0 comments on commit 8810e14

Please sign in to comment.