How to create and move to folders with server-side Javascript

How to create and move to folders with server-side Javascript

In previous article, we’ve created a Data Extension with server-side Javascript in a specific folder. But what happens if that folder doesn’t exist? Is it possible to create a new folder and move there our brand new Data Extension?

Of course it is and here is how!

Create a new folder

Creating a new folder is a matter of getting the parent folder ID of the root folder called “dataextension” and then specifiying all the core properties of the new folder.

<script runat=server>

    Platform.Load("core","1.1");

    var DE = Folder.Retrieve({Property:"ContentType",SimpleOperator:"equals",Value:"dataextension"});
    var ParentFolderID = DE[0].ID;

    var newFolder = {
        "Name" : "Brand New Folder",
        "CustomerKey" : Platform.Function.GUID(),
        "Description" : "My new awesome folder",
        "ContentType" : "dataextension",
        "IsActive" : "true",
        "IsEditable" : "true",
        "AllowChildren" : "false",
        "ParentFolderID" : ParentFolderID
    }

    try {

        var Status = Folder.Add(newFolder);
        
        if(Status == 'OK') {
            Write('(+) Folder "' + newFolder.Name + '" was created successfully.' + '<br>')
        } else {
            Write('(!) Folder "' + newFolder.Name + '" was not created. Error message: ' + Status + '<br>')
        }

    } catch(err) {
        Write('(!) Folder "' + newFolder.Name + '" was not created. Error message: ' + err + '<br>')
    }

</script>

Move a Data Extension to a folder

As for the moving part, all we need is to update the Data Extension object with a new CategoryID that we can retrieve using the name of the target folder.

<script runat=server>

    Platform.Load("core","1.1");

    var DataExtensionCustomerKey = "SampleDEkey",
        TargetFolderName = "Brand New Folder";

    var TargetFolderProperties = Folder.Retrieve({ Property: "Name", SimpleOperator: "equals", Value: TargetFolderName });
    var TargetFolderFolderID = TargetFolderProperties[0].ID;  

    var DE = DataExtension.Init(DataExtensionCustomerKey);
    var obj = {
        CategoryID : TargetFolderFolderID 
    }
    
    try {

        var Status = DE.Update(obj);

        if(Status == 'OK') {
            Write('(+) Data Extension "' + DataExtensionCustomerKey + '" was moved to "' + TargetFolderName + '" folder successfully.' + '<br>')
        } else {
            Write('(!) Data Extension "' + DataExtensionCustomerKey + '" was not moved to "' + TargetFolderName + ' folder. Error: ' + Status + '<br>')
        }
        
    } catch(err) {
        Write('(!) Data Extension "' + DataExtensionCustomerKey + '" was not moved to "' + TargetFolderName + ' folder. Error: ' + err + '<br>')
    }

</script>

Have I missed anything?

Please poke me with a sharp comment below or use the contact form.

Pay me a coffee

Want to say thanks? Pay me a coffee! Remember, I turn coffee into code.

  1. Some logic should be added to handle folders with the same name. AFAIK the Folder.Retrieve method will look at all folders (specifically Automation Studio, Data Extensions, Journey Builder and Content Builder). An additional filter to target “dataextensions” would resolve this.

Leave a Reply

Your email address will not be published. Required fields are marked *

ampscript
Up Next:

How to debug AMPscript and avoid Error 500

How to debug AMPscript and avoid Error 500