Skip to content

Commit 39b1dc0

Browse files
committed
SharePoint API: support for managing modern (communication) sites
1 parent a6382fe commit 39b1dc0

7 files changed

Lines changed: 173 additions & 26 deletions

File tree

examples/SharePoint/Sites/CreateModernSite.php

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,22 @@
11
<?php
22

3-
require_once __DIR__ . '../../vendor/autoload.php';
4-
$settings = include(__DIR__ . '../../../tests/Settings.php');
3+
require_once __DIR__ . '/../../vendor/autoload.php';
4+
$settings = include(__DIR__ . '/../../../tests/Settings.php');
55

66
use Office365\Runtime\Auth\ClientCredential;
7-
use Office365\Runtime\Http\RequestOptions;
87
use Office365\SharePoint\ClientContext;
8+
use Office365\SharePoint\Portal\SPSiteManager;
99

1010
try {
1111
$credentials = new ClientCredential($settings['ClientId'], $settings['ClientSecret']);
1212
$ctx = (new ClientContext($settings['Url']))->withCredentials($credentials);
1313

14-
$url = "{$ctx->getBaseUrl()}/_api/SPSiteManager/create";
15-
$request = new RequestOptions($url);
16-
$payload = array(
17-
"request" => array(
18-
"Title" => "Communication Site 1",
19-
"Url" => "{$ctx->getBaseUrl()}/sites/commsite2",
20-
"Lcid" => 1033,
21-
"ShareByEmailEnabled" => false,
22-
"Classification" => "Low Business Impact",
23-
"Description" => "Description",
24-
"WebTemplate" => "SITEPAGEPUBLISHING#0",
25-
"SiteDesignId" => "6142d2a0-63a5-4ba0-aede-d9fefca2c767",
26-
"Owner" => $settings['TestAccounts'][0]
27-
)
28-
);
29-
$request->Data = json_encode($payload);
30-
$resp = $ctx->getPendingRequest()->executeQueryDirect($request);
31-
$json = json_decode($resp->getContent(), true);
32-
print("Site has been created: {$json["d"]["Create"]["SiteUrl"]} \n");
14+
$siteManager = new SPSiteManager($ctx);
15+
//create communications site
16+
//refer https://docs.microsoft.com/en-us/sharepoint/dev/apis/site-creation-rest for docs
17+
$result = $siteManager->create("commsite127", $settings['TestAccountName'], "Low Business Impact");
18+
$siteManager->executeQuery();
19+
print("Site has been created: {$result->getValue()->SiteUrl} \n");
3320
}
3421
catch (Exception $e) {
3522
echo 'Failed: ', $e->getMessage(), "\n";

src/Runtime/ClientRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ public function getCurrentQuery(){
9090
/**
9191
* Add query into request queue
9292
* @param ClientAction $query
93-
* @param bool $toBegin
93+
* @param bool $executeFirst
9494
*/
95-
public function addQuery(ClientAction $query,$toBegin=false)
95+
public function addQuery(ClientAction $query, $executeFirst=false)
9696
{
97-
if($toBegin)
97+
if($executeFirst)
9898
array_unshift($this->queries , $query);
9999
else
100100
$this->queries[] = $query;

src/Runtime/ClientResult.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public function executeQuery(){
4949
}
5050

5151

52+
/**
53+
* @return bool|int|ClientObject|ClientValue|string|null
54+
*/
5255
public function getValue(){
5356
return $this->value;
5457
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Office365\SharePoint\Portal;
4+
5+
use Office365\Runtime\ClientValue;
6+
7+
class SPSiteCreationRequest extends ClientValue
8+
{
9+
10+
function getServerTypeName()
11+
{
12+
return "Microsoft.SharePoint.Portal.SPSiteCreationRequest";
13+
}
14+
15+
/**
16+
* @var string
17+
*/
18+
public $Classification;
19+
20+
/**
21+
* @var string
22+
*/
23+
public $Url;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $WebTemplate;
29+
/**
30+
* @var int
31+
*/
32+
public $Lcid;
33+
/**
34+
* @var false
35+
*/
36+
public $ShareByEmailEnabled;
37+
/**
38+
* @var string
39+
*/
40+
public $SiteDesignId;
41+
/**
42+
* @var string
43+
*/
44+
public $Title;
45+
/**
46+
* @var string
47+
*/
48+
public $Description;
49+
/**
50+
* @var string
51+
*/
52+
public $Owner;
53+
54+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Office365\SharePoint\Portal;
4+
5+
use Office365\Runtime\ClientValue;
6+
7+
class SPSiteCreationResponse extends ClientValue
8+
{
9+
function getServerTypeName()
10+
{
11+
return "Microsoft.SharePoint.Portal.SPSiteCreationResponse";
12+
}
13+
14+
15+
/**
16+
* @var string
17+
*/
18+
public $SiteId;
19+
20+
/**
21+
* @var string
22+
*/
23+
public $SiteStatus;
24+
25+
/**
26+
* @var string
27+
*/
28+
public $SiteUrl;
29+
30+
}

src/SharePoint/Portal/SPSiteManager.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,54 @@
44
namespace Office365\SharePoint\Portal;
55

66

7+
use Office365\Runtime\Actions\InvokePostMethodQuery;
8+
use Office365\Runtime\ClientResult;
9+
use Office365\Runtime\ResourcePath;
710
use Office365\SharePoint\BaseEntity;
11+
use Office365\SharePoint\ClientContext;
812

913
class SPSiteManager extends BaseEntity
1014
{
1115

12-
public function create(){
16+
public function __construct(ClientContext $ctx)
17+
{
18+
parent::__construct($ctx,new ResourcePath("SPSiteManager"));
19+
}
20+
21+
/**
22+
* @param string $title
23+
* @param string $owner
24+
* @param string $classification
25+
* @param int $lcid
26+
* @return ClientResult
27+
*/
28+
public function create($title, $owner=null, $classification=null, $lcid=1033){
29+
$request = new SPSiteCreationRequest();
30+
$request->Title = $title;
31+
$request->Description = $title;
32+
$request->Owner = $owner;
33+
$request->Url = "{$this->getContext()->getBaseUrl()}/sites/{$title}";
34+
$request->Lcid = $lcid;
35+
$request->ShareByEmailEnabled = false;
36+
$request->WebTemplate = "SITEPAGEPUBLISHING#0";
37+
$request->SiteDesignId = "6142d2a0-63a5-4ba0-aede-d9fefca2c767";
38+
$request->Classification = $classification;
39+
$qry = new InvokePostMethodQuery($this,"Create",null, "request", $request);
40+
41+
$result = new ClientResult($this->context, new SPSiteCreationResponse());
42+
$this->getContext()->addQueryAndResultObject($qry, $result);
43+
return $result;
44+
}
45+
1346

47+
/**
48+
* @param string $siteId
49+
* @return $this
50+
*/
51+
public function delete($siteId){
52+
$qry = new InvokePostMethodQuery($this,"Delete",array($siteId));
53+
$this->getContext()->addQuery($qry);
54+
return $this;
1455
}
1556

1657

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Office365;
4+
5+
use Office365\SharePoint\Portal\SPSiteCreationResponse;
6+
use Office365\SharePoint\Portal\SPSiteManager;
7+
8+
class SPSiteManagerTest extends SharePointTestCase
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private static $targetSiteId;
14+
15+
public function testCreateCommunicationSite()
16+
{
17+
$siteManager = new SPSiteManager(self::$context);
18+
$siteName = self::createUniqueName("CommSite");
19+
self::assertNotNull($siteName);
20+
$result = $siteManager->create($siteName,self::$settings["UserName"],"Low Business Impact");
21+
self::$context->executeQuery();
22+
self::assertInstanceOf(SPSiteCreationResponse::class, $result->getValue());
23+
self::$targetSiteId = $result->getValue()->SiteId;
24+
}
25+
26+
public function testDeleteCommunicationSite()
27+
{
28+
$siteManager = new SPSiteManager(self::$context);
29+
$siteManager->delete(self::$targetSiteId)->executeQuery();
30+
}
31+
32+
}

0 commit comments

Comments
 (0)