Skip to content

Commit

Permalink
SharePoint API: document set support
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrem committed Jul 15, 2023
1 parent 27b956e commit 2a82afd
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Runtime/Actions/ReadEntityQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(ClientObject $entityToRead, $includeProperties = arr
$includeProperties = array();
$expandProperties = array_filter($includeProperties, function ($name) use ($entityToRead) {
$propType = $entityToRead->getProperty($name, null, true);
return $propType instanceof ClientObject;
return $propType instanceof ClientObject || $name == "Properties";
});
if (!empty($includeProperties))
$bindingType->getQueryOptions()->Select = implode(",", $includeProperties);
Expand Down
66 changes: 63 additions & 3 deletions src/SharePoint/DocumentManagement/DocumentSet/DocumentSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,71 @@

namespace Office365\SharePoint\DocumentManagement\DocumentSet;

use Office365\Runtime\ClientObject;
use Office365\Runtime\Actions\InvokePostMethodQuery;
use Office365\Runtime\Http\HttpMethod;
use Office365\Runtime\Http\RequestOptions;
use Office365\SharePoint\ClientContext;
use Office365\SharePoint\Entity;
use Office365\SharePoint\Folder;
use Office365\SharePoint\SPList;

class DocumentSet extends ClientObject
class DocumentSet extends Entity
{


/**
* Creates a DocumentSet (section 3.1.5.3) object on the server.
*
* @param ClientContext $context
* @param Folder $parentFolder
* @param string $name
* @return DocumentSet
*/
public static function create($context, $parentFolder, $name)
{
$returnType = new DocumentSet($context);
$parentFolder->ensureProperties(array("UniqueId", "Properties", "ServerRelativeUrl"),
function () use ($parentFolder, $context, $name, $returnType) {
$customProps = $parentFolder->getProperty("Properties");
$listId = $customProps['vti_x005f_listname'];
$targetList = $context->getWeb()->getLists()->getById($listId);
$folderUrl = $parentFolder->getServerRelativeUrl() . '/' . $name;

$returnType->setProperty("ServerRelativeUrl", $folderUrl);
$targetList->ensureProperty("Title",
function () use ($targetList, $parentFolder, $folderUrl) {
DocumentSet::_create($targetList, $folderUrl);
});
});
return $returnType;
}

/**
* @param SPList $targetList
* @param string $folderUrl
* @param string $ctId
* @return void
*/
private static function _create($targetList, $folderUrl, $ctId = "0x0120D520")
{
$context = $targetList->getContext();
$qry = new InvokePostMethodQuery($targetList);
$context->addQuery($qry);
$context->getPendingRequest()->beforeExecuteRequestOnce(
function (RequestOptions $request) use ($context, $targetList, $ctId, $folderUrl) {
$request->Url = "{$context->getBaseUrl()}/_vti_bin/listdata.svc/{$targetList->getTitle()}";
$request->Method = HttpMethod::Post;
$request->ensureHeader("Slug", "{$folderUrl}|{$ctId}");
});
}

function setProperty($name, $value, $persistChanges = true)
{
if (is_null($this->resourcePath)) {
if ($name === "ServerRelativeUrl") {
$this->resourcePath = $this->getContext()->getWeb()->getFolderByServerRelativeUrl($value)->getResourcePath();
}
}
return parent::setProperty($name, $value, $persistChanges);
}

}
11 changes: 11 additions & 0 deletions src/SharePoint/Web.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ public static function getWebUrlFromPageUrl(ClientContext $ctx, $absUrl)
$ctx->addQueryAndResultObject($qry, $result);
return $result;
}

/**
* Retrieves the default document library
* @return SPList
*/
public function defaultDocumentLibrary(){
return new SPList($this->getContext(),
new ServiceOperationPath("DefaultDocumentLibrary", null,
$this->getResourcePath()));
}

/**
* @param string $logonName
* @return User
Expand Down
2 changes: 1 addition & 1 deletion tests/Settings.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

$secure_vars = explode(";",getenv("phpSPO_secure_vars"));
$tenant_prefix = $secure_vars[4];
$tenant_prefix = "mediadev8";

return array(
'TenantName' => "{$tenant_prefix}.onmicrosoft.com",
Expand Down
42 changes: 42 additions & 0 deletions tests/sharepoint/DocSetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Office365;


use Office365\SharePoint\DocumentManagement\DocumentSet\DocumentSet;

class DocSetTest extends SharePointTestCase
{

public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
}

public static function tearDownAfterClass(): void
{
parent::tearDownAfterClass();
}


public function testCreate(){
$lib = self::$context->getWeb()->defaultDocumentLibrary()->get()->executeQuery();
$this->assertNotNull($lib->getResourcePath());
$name = "DocSet_" . rand(1, 100000);
$returnType = DocumentSet::create(self::$context,$lib->getRootFolder(), $name)->executeQuery();
$this->assertNotNull($returnType->getResourcePath());
return $returnType;
}

/**
* @depends testCreate
* @param DocumentSet $docSet
*/
public function testDelete($docSet){
$docSet->deleteObject()->executeQuery();

$result = $docSet->get()->select(["Exists"])->executeQuery();
$this->assertFalse($result->getProperty("Exists"));
}

}
2 changes: 1 addition & 1 deletion tests/sharepoint/SharePointTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static function setUpBeforeClass(): void
{
self::$settings = include(__DIR__ . '/../Settings.php');
self::$testAccountName = self::$settings['TestAccountName'];
self::$context = (new ClientContext(self::$settings['Url']))
self::$context = (new ClientContext(self::$settings['TeamSiteUrl']))
->withCredentials(new UserCredentials(self::$settings['UserName'],self::$settings['Password']));
}

Expand Down

0 comments on commit 2a82afd

Please sign in to comment.