Find a Data Extension and it’s folder path using SSJS

If you work with a lot of Data Extensions in multiple folders, you know the pain of using the built-in search feature. It only searches the folder that you’re in, with the exclusion of any sub-folders, which is simply impractical.

Below, I would like to show you how to find a Data Extension and it’s path using Server-Side JavaScript: which functions to use and how to iterate through them.

If you’re not interested in the technical part and want to jump straight into building the search app, click here.

SSJS Data Extension and Folder Functions

In order to find a Data Extension, we will use two SSJS Core functions: DataExtension.Retrieve and Folder.Retrieve. The first function will help us identify the Data Extension, either by its name or the External Key. The second function retrieves an array of folders based on the specified criteria. It also retrieves details regarding the parent folder of a given folder if one exists, and that’s what we will use to build the Data Extension path.

<script runat="server">
Platform.Load("core","1.1.5");
var DEprop = //use either "Name" or "Customer Key"
var DEval = //provide either the Name or External Key
var FindDE = DataExtension.Retrieve({Property:DEprop,SimpleOperator:"equals",Value:DEval});
var FolderID = FindDE[0].CategoryID;
var DEname = FindDE[0].Name;
var list = [];
list.push(DEname);
var path = function(id) {
if (id> 0) {
var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:id});
list.unshift(results [0].Name);
return path(results[0].ParentFolder.ID);
} else {
return id;
}
};
path(FolderID);
Write(list.join("> "));
</script>

In the above script, you will need to define two variables, DEprop and DEval, depending on whether you have the name or the External Key of the Data Extension. You can paste the script on a CloudPage, define the two variables, and once you click on the “Publish” button, the script will be executed and results will be visible on-screen, without the need to actually publish the CloudPage:

Create a simple Data Extension search app

If users of your instance of Salesforce Marketing Cloud often struggle with finding Data Extensions, you can create an app on a CloudPage to help them navigate the folders. By adding a simple form, you can enable the users to perform a search from a CloudPage:

Paste the below code onto a CloudPage and publish it. Anyone with access to the link will be able to search for Data Extensions located in the Business Unit where the CloudPage was created. If the CloudPage has been created in the Parent Business Unit, they will also be able to search the Shared Data Extensions folder.

<table style="padding: 20px;"><tr><td>
<b>How would you like to identify the Data Extension?</b><br>
<form action="%%=RequestParameter('PAGEURL')=%%" method="post">
<select name="DEprop">
<option value="Name">Name</option>
<option value="CustomerKey">External Key</option>
</select>
equals
<input type="text" name="DEval" value="" maxlength="128"><br>
<input type="submit" value="Submit">
</form><br><b>Folder path: </b>
<script runat="server">
Platform.Load("core","1.1.5");
var DEprop = Request.GetQueryStringParameter("DEprop");
var DEval = Request.GetQueryStringParameter("DEval");
var FindDE = DataExtension.Retrieve({Property:DEprop,SimpleOperator:"equals",Value:DEval});
var FolderID = FindDE[0].CategoryID;
var DEname = FindDE[0].Name;
var list = [];
list.push(DEname);
var path = function(id) {
if (id> 0) {
var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:id});
list.unshift(results [0].Name);
return path(results[0].ParentFolder.ID);
} else {
return id;
}
};
path(FolderID);
Write(list.join("> "));
</script>
</td></tr></table>

Last but not least, always remember to put security measures into practice when setting up this kind of functionality on a CloudPage, to prevent your data from being exposed in a breach.

PS. There is a free tool available called SFMC Object Finder. It’s a Chrome extension that lets you search for a variety of objects, like images, data extensions and other assets, in your instance of Salesforce Marketing Cloud. All it requires is that you have an active session of Salesforce Marketing Cloud open in your Chrome browser. However, if you cannot install this extension or need to be able to find Data Extensions programmatically, you can use Server-Side JavaScript to achieve it!

14 thoughts on “Find a Data Extension and it’s folder path using SSJS

  1. NewJackSwing4Ever

    As another previous user requested, I would like to know how to retrieve location for Automations from Automation Studio among other objects such as EmailSendDefinition, TriggeredSendDefinition, QueryActivity, and FilterActivity. The source code works beautifully for DataExtension object. However, substituting the other object names for DataExtension shows no path even though I am able to do retrieves for other common fields such as Name, CustomerKey, and CategoryID.

    Like

  2. webdesignmx05

    The source code works beautifully for DataExtension object. Like another previous user requested for QueryActivity, I would like to know how to retrieve location for Automations from Automation Studio among other objects such as EmailSendDefinition, TriggeredSendDefinition, QueryActivity, and FilterActivity. However, substituting the other object names for DataExtension shows no path even though I am able to do retrieves for other common fields such as Name, CustomerKey, and CategoryID. Btw, there is a SFMC Object Finder Chrome plugin that will locate the path of different objects on a singular basic. It would be nice to utilize this technique using array list of objects and display a table showing the full folder paths next to all objects from the array List. Thanks.

    Like

  3. SquadronBlue

    I am trying to use similar approach for Content Area folder locator by using ContentAreaObj.Retrieve({Property:DEprop,SimpleOperator:”equals”,Value:DEval}); which doesn’t seem to be working.

    Any clues for what approach should i take ?

    Like

  4. Pingback: SFMC Data Extension Inventory (Take 2) | Gortonington

  5. ZeRoberto

    Contrary to what it says in the description, this doesn’t find anything in the Shared Folders of a Parent Folder. Nice idea but not very practical

    Like

  6. Julio

    I’m not able to get this to work. I just pasted the above script in a Cloudpage (landing page) and updated DEprop and DEval with the name of the Data extension and when I hit publish it gives me an error.

    Like

Leave a comment