[SalesForce] How to access shared data extensions with FuelSDK

I am using the Fuel SDK for PHP with an Enterprise 2.0 account. I have created client credentials for both the parent and child business units. My question is now: how do I work with Shared Data Extensions? I don't see any of the shared data extensions when I do a Get for data extensions using either the parent or child business unit credentials..

I also do not see any of the Shared Data Extension folders when I do a get for data extension folders in either the parent or child business unit credentials.

I cannot find any documentation or examples that cover this.

Best Answer

I have figured out how to do this. In the current version of the FuelSDK for PHP you cannot directly list shared data extensions, what you have to do is to get all the folders that are of ContentType shared_dataextension and then retrieve the data extensions within those folders by setting a filter.

The solution I have settled on is to get all of the shared_dataextension folders and put their IDs into an array. Then I get all of the data extensions whose CategoryID (the folder id) is in that list of shared data extension folders by using a simple filter with an IN operator.

Note: in order to see/access the shared items you need to be using a set of OAuth credentials that are associated to the parent business unit. Credentials that are associated with child business units cannot see shared items. This means that if you need to work with both shared items and non-shared items in child business units you will have to juggle multiple sets of credentials.

Sample code:

<?php
require('sdk/ET_Client.php');
try {
    $myclient = new ET_Client();

    // Retrieve All Folders with GetMoreResults
    print "Retrieve shared data extension folders and the data extensions inside of them. \n";
    $getFolder = new ET_Folder();
    $getFolder->authStub = $myclient;
    $getFolder->props = array("ID", "Client.ID", "ParentFolder.ID", "ParentFolder.CustomerKey", "ParentFolder.ObjectID", "ParentFolder.Name", "ParentFolder.Description", "ParentFolder.ContentType", "ParentFolder.IsActive", "ParentFolder.IsEditable", "ParentFolder.AllowChildren", "Name", "Description", "ContentType", "IsActive", "IsEditable", "AllowChildren", "CreatedDate", "ModifiedDate", "Client.ModifiedBy", "ObjectID", "CustomerKey", "Client.EnterpriseID", "Client.CreatedBy");
    $getFolder->filter = array('Property' => 'ContentType','SimpleOperator' => 'equals','Value' => "shared_dataextension");
    $getResponse = $getFolder->get();
    $folders = array();
    foreach($getResponse->results as $r)
    {
        if(!in_array($r->ID, $folders, true)) {
            $folders[] = $r->ID;
        }
    }
    while($getResponse->moreResults)
    {
        $getResponse = $getFolder->GetMoreResults();
        foreach($getResponse->results as $r)
        {
            if(!in_array($r->ID, $folders, true)) {
                $folders[] = $r->ID;
            }
        }
    }

    print "<hr/>Folders:</br/><pre>";
    print_r($folders);
    print "</pre><hr/>";
    $des = array();
    $getDE = new ET_DataExtension();
    $getDE->authStub = $myclient;
    $getDE->props = array("CustomerKey", "Name", "CategoryID");
    $getDE->filter = array('Property' => 'CategoryID','SimpleOperator' => 'IN','Value' => $folders);
    $getResult = $getDE->get();
    $des[] = $getResult->results;
    while($getResult->moreResults) {
        $getResult = $getDE->getMoreResults();
        $des[] = $getResult->results;
    }
    print 'Data Extensions: '."\n";
    print "<pre>";
    print_r($des);
    print "</pre>";


}
catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

?>