Skip to content

Instantly share code, notes, and snippets.

@VijayaSankarN
Last active January 17, 2023 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VijayaSankarN/73427c19dcdc1dfb2c4d6ae70e0da764 to your computer and use it in GitHub Desktop.
Save VijayaSankarN/73427c19dcdc1dfb2c4d6ae70e0da764 to your computer and use it in GitHub Desktop.
<script runat="server" language="JavaScript">
Platform.Load("core","1.1.2");
try {
var DE_CustomerKey = Request.GetQueryStringParameter("de_key");
var DE = DataExtension.Init(DE_CustomerKey);
var DEName = DataExtension.Retrieve({Property:"CustomerKey",SimpleOperator:"equals",Value:DE_CustomerKey})[0].Name;
var DEFields = DE.Fields.Retrieve();
var DEFieldsList = [];
for (var i = 0; i < DEFields.length; i++) {
DEFieldsList.push(DEFields[i].Name);
}
Write(DEFieldsList.join());
Write("\r\n");
var DERows = DE.Rows.Retrieve();
for (var i = 0; i < DERows.length; i++) {
var DERowsList = [];
var record = DERows[i];
for(var j = 0; j < DEFieldsList.length; j++) {
DERowsList.push(record[DEFieldsList[j]]);
}
Write(DERowsList.join());
Write("\r\n");
}
HTTPHeader.SetValue("content-disposition","attachment; filename=" + DEName + ".csv");
} catch(e) {
Write(Stringify(e));
}
</script>
<script runat="server" language="JavaScript">
Platform.Load("core","1.1.2");
try {
var DESearchOperand = Request.GetQueryStringParameter("DESearchOperand") || "CustomerKey";
var DENameOperator = Request.GetQueryStringParameter("DENameOperator") || "equals";
var DENameOperand = Request.GetQueryStringParameter("DENameOperand") || null;
Variable.SetValue("DESearchOperand", DESearchOperand);
Variable.SetValue("DENameOperator", DENameOperator);
Variable.SetValue("DENameOperand", DENameOperand);
if(DENameOperand != null) {
var DEList = DataExtension.Retrieve({
Property: DESearchOperand,
SimpleOperator: DENameOperator,
Value: (DENameOperator == "in" ? DENameOperand.split(",") : DENameOperand)
});
var DETableRows = [];
for (var i = 0; i < DEList.length; i++) {
// Name
var tempVar = "<td>" + DEList[i].Name + "</td>";
tempVar += "<td>" + DEList[i].CustomerKey + "</td>";
// Folder Path
var folderPath = [];
var path = function(id) {
if (id> 0) {
var results = Folder.Retrieve({Property:"ID",SimpleOperator:"equals",Value:id});
folderPath.unshift(results [0].Name);
return path(results[0].ParentFolder.ID);
} else {
return id;
}
};
path(DEList[i].CategoryID);
tempVar += "<td>" + folderPath.join(" ⇒ ") + "</td>";
// Operation
var operationString = '<a href="https://abc.xyz/downloadCSV?de_key=' + DEList[i].CustomerKey + '" title="Export CSV">⇩</a>';
operationString += '<a href="https://abc.xyz/viewInTable?de_key=' + DEList[i].CustomerKey + '" title="View in Table">👁</a>';
tempVar += "<td>" + operationString + "</td>";
DETableRows.push(tempVar);
}
Variable.SetValue("DETableRows", DETableRows);
}
} catch(e) {
Write(Stringify(e));
}
</script>
<style>
a {
text-decoration: none;
padding-right: 0.5rem;
}
</style>
<h1 style="text-align: center">DATA EXTENSION MANAGER</h1>
<form method="post">
Get Data Extension(s) whose
<select name="DESearchOperand">
<option %%=IIF(@DESearchOperand=="CustomerKey", "Selected", "")=%% value="CustomerKey" %%=IIF(@DESearchOperand=="CustomerKey", "Selected", "")=%%>External Key</option>
<option value="Name" %%=IIF(@DESearchOperand=="Name", "Selected", "")=%%>Data Extension Name</option>
</select>
<select name="DENameOperator">
<option %%=IIF(@DENameOperator=="equals", "Selected", "")=%%>equals</option>
<option %%=IIF(@DENameOperator=="notEquals", "Selected", "")=%%>notEquals</option>
<option %%=IIF(@DENameOperator=="in", "Selected", "")=%%>in</option>
<option %%=IIF(@DENameOperator=="like", "Selected", "")=%%>like</option>
</select>
<input type="text" name="DENameOperand" value="%%=v(@DENameOperand)=%%">
<input type="submit">
</form>
%%[ IF ROWCOUNT(@DETableRows) > 0 THEN ]%%
<table border="1" cellpadding="7">
<thead>
<th>Data Extension Name</th>
<th>Data Extension External Key</th>
<th>Path</th>
<th>Operation</th>
</thead>
<tbody>
%%[
FOR @I = 1 TO ROWCOUNT(@DETableRows) DO
]%%
<tr>
%%=v(Row(@DETableRows, @I))=%%
</tr>
%%[ NEXT @I ]%%
</tbody>
</table>
%%[ ENDIF ]%%
<script runat="server" language="JavaScript">
Platform.Load("core","1.1.2");
try {
var DE_CustomerKey = Request.GetQueryStringParameter("de_key");
var DE = DataExtension.Init(DE_CustomerKey);
var DEName = DataExtension.Retrieve({Property:"CustomerKey",SimpleOperator:"equals",Value:DE_CustomerKey})[0].Name;
var DEFields = DE.Fields.Retrieve();
var DEFieldsList = [];
Write("<table border='1' cellpadding='7'>");
Write("<th>");
for (var i = 0; i < DEFields.length; i++) {
DEFieldsList.push(DEFields[i].Name);
}
Write(DEFieldsList.join("</th><th>"));
Write("</th>");
var DERows = DE.Rows.Retrieve();
for (var i = 0; i < DERows.length; i++) {
var DERowsList = [];
var record = DERows[i];
Write("<tr><td>");
for(var j = 0; j < DEFieldsList.length; j++) {
DERowsList.push(record[DEFieldsList[j]]);
}
Write(DERowsList.join("</td><td>"));
Write("</td></tr><br/>");
}
Write("</table>");
} catch(e) {
Write(Stringify(e));
}
</script>
@cvvishalkumar
Copy link

Thanks for sharing such a wonderful and helpful article :)

Can you also add the code for "viewCSV" code block, which would help the community.

@VijayaSankarN
Copy link
Author

@cvvishalkumar, those are the cloud pages that we need to create and embed these blocks into it. Here's the explanation on making these to work: https://vijayasankarn.wordpress.com/2021/01/18/marketing-cloud-programmatically-export-data-extension/

@VijayaSankarN
Copy link
Author

Thanks for sharing such a wonderful and helpful article :)

Can you also add the code for "viewCSV" code block, which would help the community.

It has been added now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment