22
33namespace 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}
0 commit comments