I am creating a Landing Page in Marketing Cloud and would like to use the API to query an Object and filter by a specific property, returning only relevant results.
How can I do this?
Use the following code to start your API call. This example uses a EmailSendDefinition Object and a few properties. Refer here Marketing Cloud Technical Library for more information on the properties available. Make sure to include the Property that you need to filter by (in this case, "ModifiedDate")
<script runat="server">
Platform.Load("Core", "1.1.1");
var rr = Platform.Function.CreateObject("RetrieveRequest");
Platform.Function.SetObjectProperty(rr, "ObjectType", "EmailSendDefinition");
Platform.Function.AddObjectArrayItem(rr, "Properties", "CustomerKey");
Platform.Function.AddObjectArrayItem(rr, "Properties", "Name");
Platform.Function.AddObjectArrayItem(rr, "Properties", "EmailSubject")
Platform.Function.AddObjectArrayItem(rr, "Properties", "SendDefinitionList");
Platform.Function.AddObjectArrayItem(rr, "Properties", "DeliveryProfile.CustomerKey");
Platform.Function.AddObjectArrayItem(rr, "Properties", "SenderProfile.CustomerKey");;
Platform.Function.AddObjectArrayItem(rr, "Properties", "IsMultipart");
Platform.Function.AddObjectArrayItem(rr, "Properties", "CategoryID");
Platform.Function.AddObjectArrayItem(rr, "Properties", "CreatedDate");
Platform.Function.AddObjectArrayItem(rr, "Properties", "ModifiedDate");
Then use this code to create the ComplexFilterPart (which consists of two SimpleFilterParts)
//"From" date
var sfp1 = Platform.Function.CreateObject("SimpleFilterPart");
Platform.Function.SetObjectProperty(sfp1, "Property", "ModifiedDate");
Platform.Function.SetObjectProperty(sfp1, "SimpleOperator", "greaterThanOrEqual");
Platform.Function.AddObjectArrayItem(sfp1, "Value", "2015-08-27T00:00:00.000");
//"To" date
var sfp2 = Platform.Function.CreateObject("SimpleFilterPart");
Platform.Function.SetObjectProperty(sfp2, "Property", "ModifiedDate");
Platform.Function.SetObjectProperty(sfp2, "SimpleOperator", "lessThanOrEqual");
Platform.Function.AddObjectArrayItem(sfp2, "Value", "2015-08-28T00:00:00.000");
//Adding two Simple filters together to create a Complex filter
var cfp = Platform.Function.CreateObject("ComplexFilterPart");
Platform.Function.SetObjectProperty(cfp, "LeftOperand", sfp1);
Platform.Function.SetObjectProperty(cfp, "LogicalOperator", "AND");
Platform.Function.SetObjectProperty(cfp, "RightOperand", sfp2);
//Add the Complex filter to the RetrieveRequest
Platform.Function.SetObjectProperty(rr, "Filter", cfp);
Now perform the retrieve:
var retrieveStatus = [0, 0, 0];
var retrieveResult = Platform.Function.InvokeRetrieve(rr, retrieveStatus);
Write(Stringify(retrieveResult));
</script>
This returns a JSON object of all EmailSendDefinition Objects (User Initiated Sends) with a ModifiedDate between 27 August 2015 and 28 August 2015.
For more information on the operators available, refer here: Marketing Cloud Simple Operators