Garethp / php-ews

PHP Exchange Web Services
BSD 3-Clause "New" or "Revised" License
112 stars 45 forks source link

Create Folder in sub folder #207

Closed tds4u closed 3 years ago

tds4u commented 3 years ago

Hello,

I think the code for creating a folder isn't really working. I tried, but no success.

// Build the request object.
$request = new \garethp\ews\API\Message\CreateFolderType();
$request->Folders = new \garethp\ews\API\Type\NonEmptyArrayOfFoldersType();

$parent = new \garethp\ews\API\Type\TargetFolderIdType();
$parent->FolderId = $current;
//$parent->DistinguishedFolderId = new \garethp\ews\API\tYPE\DistinguishedFolderIdType();
//$parent->DistinguishedFolderId->Id = \garethp\ews\API\Enumeration\DistinguishedFolderIdNameType::INBOX;
$request->ParentFolderId = $parent;

$folder = new \garethp\ews\API\Type\FolderType();
$folder->DisplayName = $a;
$folder->FolderClass = 'IPF.Note';
$request->Folders->Folder = array($folder);

$current = $this->class->getClient()->CreateFolder($request);

The problem is them empty folder section "</ns2:Folders>".

SOAP XML:

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
    xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://schemas.microsoft.com/exchange/services/2006/types"
    xmlns:ns2="http://schemas.microsoft.com/exchange/services/2006/messages">
    <SOAP-ENV:Header>
        <ns1:RequestServerVersion Version="Exchange2013"/>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body>
        <ns2:CreateFolder>
            <ns2:ParentFolderId>
                <ns1:FolderId Id="AQMkAGM1MjQ4ZjE3LTczMTUtNDgzYy1iN2ExLTZmM2FmNWUwNjAzZgAuAAADnCxhZvrvm0Wv57xwzfAyagEA2glixKWdME6GH/4IWxQ8ngABPXqDKgAAAA==" ChangeKey="AQAAABYAAADaCWLEpZ0wToYf/ghbFDyeAAE93Z38"/>
            </ns2:ParentFolderId>
            <ns2:Folders>
                <ns1:Folder/>
            </ns2:Folders>
        </ns2:CreateFolder>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

If I manually add the following lines it works:

<ns2:Folders>
    <ns1:Folder>
        <ns1:DisplayName>ABC</ns1:DisplayName>
    </ns1:Folder>
</ns2:Folders>
Garethp commented 3 years ago

Have you tried just using the createFolders() function? It should be something like this:

$folderId = new \garethp\ews\API\Type\FolderIdType($folderId, $changeKey);

$api->createFolders($a, $folderId, [], 'IPF.Note');

Here's an example of how I do it in my tests. If you still wanted to build the request yourself, you'd do it a lot simpler, the way that createFolders does anyway:

$folderId = new \garethp\ews\API\Type\FolderIdType($folderId, $changeKey);

$api->getClient()->CreateFolder([
    'ParentFolderId' => $folderId->toArray(true),
    'Folders' => ['Folder' => [ 'DisplayName' => $a, 'FolderClass' => 'IFP.Note' ] ]
]);

You don't need to create all those classes manually the way you do

tds4u commented 3 years ago

Here's an example of how I do it in my tests. If you still wanted to build the request yourself, you'd do it a lot simpler, the way that createFolders does anyway:

$folderId = new \garethp\ews\API\Type\FolderIdType($folderId, $changeKey);

$api->getClient()->CreateFolder([
    'ParentFolderId' => $folderId->toArray(true),
    'Folders' => ['Folder' => [ 'DisplayName' => $a, 'FolderClass' => 'IFP.Note' ] ]
]);

That works! Thx ;-)