addMessageFolders

The addMessageFolders function allows you to add a new message folders to the account. Message folders can be used to group messages.

Syntax

writeResult = bApi.addMessageFolders(messageFolderObject[] messageFolders);

Attributes

Name Type Required Description
name string Yes The name assigned to the folder.
parentId string No; default is the root (top-level) folder The unique id assigned to the parent folder which contains this folder.

PHP Code Example

<?php
/**
 * This script will add a new message folder that is a sub/child 
 * folder of an existing message folder. It will then print a
 * visual representation of the folder structure.
 *
 * @copyright  Copyright (c) 2011 Bronto Software (http://www.bronto.com)
 */
 
$client = new SoapClient('https://api.bronto.com/v4?wsdl', array('trace' => 1, 
                                 'features' => SOAP_SINGLE_ELEMENT_ARRAYS));
 
try {
  // Add a valid API token
  $token = "ADD API TOKEN HERE";
 
  print "logging in\n";
  $sessionId = $client->login(array('apiToken' => $token))->return;
 
  $session_header = new SoapHeader("http://api.bronto.com/v4",
                   'sessionHeader',
                   array('sessionId' => $sessionId));
  $client->__setSoapHeaders(array($session_header));
 
  // Get the ID of the parent folder we want to add 
  // a sub/child folder under. We are filtering based
  // on the name of the parent folder.
  // Be sure to replace the value with the name of the 
  // parent folder you want an ID for.
 
  print "Reading message folders that match the given filter\n";
 
  $filter = array('type' => 'OR',
          'name' => array('operator' => 'EqualTo',
                  'value' => 'NAME OF MESSAGE FOLDER'
            )
           );
 
  $folders = $client->readMessageFolders(array('pageNumber' => 1,
                         'filter' => $filter,
                         )
                       )->return; 
  foreach ($folders as $folder) {
    print "Parent folder ID: " . $folder->id . "\n";
  }  
 
  // Add a nested message folder that is a 
  // sub/child folder of the existing 
  // message folder we got an ID for above.
  // You may want to replace the name with a 
  // more relevant message folder name.
 
  $messageFolders = array('name' => 'Child Folder',
                'parentId' => $folder->id);
 
  print "Adding the child message folder\n";
  $write_result = $client->addMessageFolders(array($messageFolders)
                    )->return;
 
  if ($write_result->errors) {
    print "There was a problem adding the message folder:\n";
    print_r($write_result->results);
  } else {
 
    // Upon a successful add, we are going to return the ID of
    // the newly created folder. We will also return a 
    // visual representation of the message folder structure
    // with the newly created message folder included.
 
    print "The message folder has been created.  Id: " . $write_result->results[0]->id . "\n";
    print "Now let's have a look at the folder structure:\n";
 
    // get all folders
    $filter = array();
 
    print "reading message folders\n";
    $folders = $client->readMessageFolders(array('pageNumber' => 1,
                           'filter' => $filter,
                           )
                         )->return;
 
      // Find the root first
      $rootFolder = null;
      foreach($folders as $folder) {
        if (!$folder->parentId) {
          $rootFolder = $folder;
          break;
        }
      }
 
      // Start building up a visual tree view of the folders
      $folderStack = array(array('folder' => $rootFolder,
                     'depth' => 0));
 
      $output = "";
      while (count($folderStack) > 0) {
        $item = array_pop($folderStack);
        $currentFolder = $item['folder'];
        $depth = $item['depth'];
        $indent = str_repeat("    ", $depth);
        $output .= $indent . $currentFolder->name . "\n";
        foreach ($folders as $folder) {
          if ($folder->parentId == $currentFolder->id) {
            array_push($folderStack, array('folder' => $folder, 'depth' => $depth + 1));
          }
        }
      }
 
      // Print out the tree
      print $output . "\n";
  } // End else
 
} catch (Exception $e) {
  print "uncaught exception\n";
  print_r($e);
}
?>