Skip to content

Instantly share code, notes, and snippets.

@wvpv
Created December 12, 2023 18:11
Show Gist options
  • Save wvpv/43c2e35992087943c4af365f80284376 to your computer and use it in GitHub Desktop.
Save wvpv/43c2e35992087943c4af365f80284376 to your computer and use it in GitHub Desktop.
SSJS List Functions
<script runat="server" language="JavaScript">
Platform.Load("core","1");
var debug = true;
var prox = new Script.Util.WSProxy();
var mid = Platform.Function.AuthenticatedMemberID();
var publicationLists = getAllPublicationLists();
if (debug) {
Write("<hr>getAllPublicationLists: " + Stringify(publicationLists));
}
var publicPublicationLists = getAllPublicPublicationLists();
if (debug) {
Write("<hr>getAllPublicPublicationLists: " + Stringify(publicPublicationLists));
}
var publicLists = getAllPublicLists();
if (debug) {
Write("<hr>getAllPublicLists: " + Stringify(publicLists));
}
var lists = getAllLists();
if (debug) {
Write("<hr>getAllLists: " + Stringify(lists));
}
var subscriberLists = getSubscriberLists("[email protected]");
if (debug) {
Write("<hr>subscriberLists: " + Stringify(subscriberLists));
}
function getRetrieveableProperties(objNameStr) {
var propArr = [];
try {
var describe = prox.describe(objNameStr);
for (var i = 0; i < describe.Results[0].Properties.length; i++) {
if (describe.Results[0].Properties[i].IsRetrievable) {
propArr.push(describe.Results[0].Properties[i].Name);
}
}
} catch (e) {
if (debug) {
Write("<br>getRetrieveableProperties exception: " + Stringify(e));
}
}
if (debug) {
Write("<hr>"+objNameStr+" propArr: " + Stringify(propArr));
}
return propArr;
}
function getAllPublicationLists(mid) {
var results = []
var obj = "List";
//var cols = getRetrieveableProperties(obj)
var cols = ["ID", "ObjectID", "PartnerKey", "CreatedDate", "ModifiedDate", "Client.ID", "Client.PartnerClientKey", "ListName", "Description", "Category", "Type", "CustomerKey", "ListClassification", "AutomatedEmail.ID"]
var filter = {Property: "ListClassification", SimpleOperator: "equals", Value: "PublicationList"};
try {
if (mid) {
prox.resetClientIds();
prox.setClientId({"ID": mid});
}
var rows = prox.retrieve(obj, cols, filter);
results = rows.Results;
} catch (e) {
if (debug) {
Write("<br>getAllPublicationLists exception: " + Stringify(e));
}
}
return results;
}
function getAllPublicPublicationLists(mid) {
var results = []
var obj = "List";
//var cols = getRetrieveableProperties(obj)
var cols = ["ID", "ObjectID", "PartnerKey", "CreatedDate", "ModifiedDate", "Client.ID", "Client.PartnerClientKey", "ListName", "Description", "Category", "Type", "CustomerKey", "ListClassification", "AutomatedEmail.ID"]
var sfp1 = {Property: "Type", SimpleOperator: "equals", Value: "Public"};
var sfp2 = {Property: "ListClassification", SimpleOperator: "equals", Value: "PublicationList"};
var cfp1 = {"LeftOperand": sfp1, "LogicalOperator": "AND", "RightOperand": sfp2};
var filter = cfp1;
try {
if (mid) {
prox.resetClientIds();
prox.setClientId({"ID": mid});
}
var rows = prox.retrieve(obj, cols, filter);
results = rows.Results;
} catch (e) {
if (debug) {
Write("<br>getAllPublicationLists exception: " + Stringify(e));
}
}
return results;
}
function getAllPublicLists(mid) {
var results = []
var obj = "List";
//var cols = getRetrieveableProperties(obj)
var cols = ["ID", "ObjectID", "PartnerKey", "CreatedDate", "ModifiedDate", "Client.ID", "Client.PartnerClientKey", "ListName", "Description", "Category", "Type", "CustomerKey", "ListClassification", "AutomatedEmail.ID"]
var sfp1 = {Property: "Type", SimpleOperator: "equals", Value: "Public"};
var sfp2 = {Property: "ListClassification", SimpleOperator: "equals", Value: "ExactTargetList"};
var cfp1 = {"LeftOperand": sfp1, "LogicalOperator": "AND", "RightOperand": sfp2};
var filter = cfp1;
try {
if (mid) {
prox.resetClientIds();
prox.setClientId({"ID": mid});
}
var rows = prox.retrieve(obj, cols, filter);
results = rows.Results;
} catch (e) {
if (debug) {
Write("<br>getAllPublicLists exception: " + Stringify(e));
}
}
return results;
}
function getAllLists(mid) {
var results = []
var obj = "List";
//var cols = getRetrieveableProperties(obj)
var cols = ["ID", "ObjectID", "PartnerKey", "CreatedDate", "ModifiedDate", "Client.ID", "Client.PartnerClientKey", "ListName", "Description", "Category", "Type", "CustomerKey", "ListClassification", "AutomatedEmail.ID"]
var filter = null
try {
if (mid) {
prox.resetClientIds();
prox.setClientId({"ID": mid});
}
var rows = prox.retrieve(obj, cols, filter);
results = rows.Results;
} catch (e) {
if (debug) {
Write("<br>getAllLists exception: " + Stringify(e));
}
}
return results;
}
function getSubscriberLists(subscriberKey) {
var results = [];
var obj = "ListSubscriber";
//var cols = getRetrieveableProperties(obj)
var cols = ["ObjectID", "SubscriberKey", "CreatedDate", "ModifiedDate", "Client.ID", "Client.PartnerClientKey", "ListID", "Status", "UnsubscribedDate", "ID"]
var filter = {Property: "SubscriberKey", SimpleOperator: "equals", Value: subscriberKey};
try {
if (mid) {
prox.resetClientIds();
prox.setClientId({"ID": mid});
}
var rows = prox.retrieve(obj, cols, filter);
results = rows.Results;
} catch (e) {
if (debug) {
Write("<br>getSubscriberLists exception: " + Stringify(e));
}
}
// Alternative: retrieve lists via Subscriber object
// try {
// var props = getRetrieveableProperties("Subscriber")
// var subscriber = Subscriber.Init(subscriberKey);
// results = subscriber.Lists.Retrieve();
// } catch (e) {
// if (debug) {
// Write("<br>getSubscriberLists exception: " + Stringify(e));
// }
// }
return results;
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment