Skip to content

Commit 2a82afd

Browse files
committed
SharePoint API: document set support
1 parent 27b956e commit 2a82afd

6 files changed

Lines changed: 119 additions & 6 deletions

File tree

src/Runtime/Actions/ReadEntityQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(ClientObject $entityToRead, $includeProperties = arr
1919
$includeProperties = array();
2020
$expandProperties = array_filter($includeProperties, function ($name) use ($entityToRead) {
2121
$propType = $entityToRead->getProperty($name, null, true);
22-
return $propType instanceof ClientObject;
22+
return $propType instanceof ClientObject || $name == "Properties";
2323
});
2424
if (!empty($includeProperties))
2525
$bindingType->getQueryOptions()->Select = implode(",", $includeProperties);

src/SharePoint/DocumentManagement/DocumentSet/DocumentSet.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,71 @@
22

33
namespace Office365\SharePoint\DocumentManagement\DocumentSet;
44

5-
use Office365\Runtime\ClientObject;
5+
use Office365\Runtime\Actions\InvokePostMethodQuery;
6+
use Office365\Runtime\Http\HttpMethod;
7+
use Office365\Runtime\Http\RequestOptions;
8+
use Office365\SharePoint\ClientContext;
9+
use Office365\SharePoint\Entity;
10+
use Office365\SharePoint\Folder;
11+
use Office365\SharePoint\SPList;
612

7-
class DocumentSet extends ClientObject
13+
class DocumentSet extends Entity
814
{
915

10-
16+
/**
17+
* Creates a DocumentSet (section 3.1.5.3) object on the server.
18+
*
19+
* @param ClientContext $context
20+
* @param Folder $parentFolder
21+
* @param string $name
22+
* @return DocumentSet
23+
*/
24+
public static function create($context, $parentFolder, $name)
25+
{
26+
$returnType = new DocumentSet($context);
27+
$parentFolder->ensureProperties(array("UniqueId", "Properties", "ServerRelativeUrl"),
28+
function () use ($parentFolder, $context, $name, $returnType) {
29+
$customProps = $parentFolder->getProperty("Properties");
30+
$listId = $customProps['vti_x005f_listname'];
31+
$targetList = $context->getWeb()->getLists()->getById($listId);
32+
$folderUrl = $parentFolder->getServerRelativeUrl() . '/' . $name;
33+
34+
$returnType->setProperty("ServerRelativeUrl", $folderUrl);
35+
$targetList->ensureProperty("Title",
36+
function () use ($targetList, $parentFolder, $folderUrl) {
37+
DocumentSet::_create($targetList, $folderUrl);
38+
});
39+
});
40+
return $returnType;
41+
}
42+
43+
/**
44+
* @param SPList $targetList
45+
* @param string $folderUrl
46+
* @param string $ctId
47+
* @return void
48+
*/
49+
private static function _create($targetList, $folderUrl, $ctId = "0x0120D520")
50+
{
51+
$context = $targetList->getContext();
52+
$qry = new InvokePostMethodQuery($targetList);
53+
$context->addQuery($qry);
54+
$context->getPendingRequest()->beforeExecuteRequestOnce(
55+
function (RequestOptions $request) use ($context, $targetList, $ctId, $folderUrl) {
56+
$request->Url = "{$context->getBaseUrl()}/_vti_bin/listdata.svc/{$targetList->getTitle()}";
57+
$request->Method = HttpMethod::Post;
58+
$request->ensureHeader("Slug", "{$folderUrl}|{$ctId}");
59+
});
60+
}
61+
62+
function setProperty($name, $value, $persistChanges = true)
63+
{
64+
if (is_null($this->resourcePath)) {
65+
if ($name === "ServerRelativeUrl") {
66+
$this->resourcePath = $this->getContext()->getWeb()->getFolderByServerRelativeUrl($value)->getResourcePath();
67+
}
68+
}
69+
return parent::setProperty($name, $value, $persistChanges);
70+
}
1171

1272
}

src/SharePoint/Web.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ public static function getWebUrlFromPageUrl(ClientContext $ctx, $absUrl)
7575
$ctx->addQueryAndResultObject($qry, $result);
7676
return $result;
7777
}
78+
79+
/**
80+
* Retrieves the default document library
81+
* @return SPList
82+
*/
83+
public function defaultDocumentLibrary(){
84+
return new SPList($this->getContext(),
85+
new ServiceOperationPath("DefaultDocumentLibrary", null,
86+
$this->getResourcePath()));
87+
}
88+
7889
/**
7990
* @param string $logonName
8091
* @return User

tests/Settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

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

66
return array(
77
'TenantName' => "{$tenant_prefix}.onmicrosoft.com",

tests/sharepoint/DocSetTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Office365;
4+
5+
6+
use Office365\SharePoint\DocumentManagement\DocumentSet\DocumentSet;
7+
8+
class DocSetTest extends SharePointTestCase
9+
{
10+
11+
public static function setUpBeforeClass(): void
12+
{
13+
parent::setUpBeforeClass();
14+
}
15+
16+
public static function tearDownAfterClass(): void
17+
{
18+
parent::tearDownAfterClass();
19+
}
20+
21+
22+
public function testCreate(){
23+
$lib = self::$context->getWeb()->defaultDocumentLibrary()->get()->executeQuery();
24+
$this->assertNotNull($lib->getResourcePath());
25+
$name = "DocSet_" . rand(1, 100000);
26+
$returnType = DocumentSet::create(self::$context,$lib->getRootFolder(), $name)->executeQuery();
27+
$this->assertNotNull($returnType->getResourcePath());
28+
return $returnType;
29+
}
30+
31+
/**
32+
* @depends testCreate
33+
* @param DocumentSet $docSet
34+
*/
35+
public function testDelete($docSet){
36+
$docSet->deleteObject()->executeQuery();
37+
38+
$result = $docSet->get()->select(["Exists"])->executeQuery();
39+
$this->assertFalse($result->getProperty("Exists"));
40+
}
41+
42+
}

tests/sharepoint/SharePointTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function setUpBeforeClass(): void
2828
{
2929
self::$settings = include(__DIR__ . '/../Settings.php');
3030
self::$testAccountName = self::$settings['TestAccountName'];
31-
self::$context = (new ClientContext(self::$settings['Url']))
31+
self::$context = (new ClientContext(self::$settings['TeamSiteUrl']))
3232
->withCredentials(new UserCredentials(self::$settings['UserName'],self::$settings['Password']));
3333
}
3434

0 commit comments

Comments
 (0)