Create a Query Activity

Why Create a Query Activity 

Creating a query activity allows you to retrieve tracking information from your account based on criteria specified in your call.

How to Create a Query Activity 

Use the sample code below as a model to create your own API call. You can’t retrieve the status or completion time of the query activity.

Sample .NET Code 

1private void button12_Click(object sender, EventArgs e){
2    SoapClient framework = new SoapClient();
3    framework.ClientCredentials.UserName.UserName = Properties.Settings.Default.UN_S1;
4    framework.ClientCredentials.UserName.Password = Properties.Settings.Default.PW_S1;
5    String requestID;
6    String status;
7    QueryDefinition qd = new QueryDefinition();
8    qd.Name = "New Query333";
9    qd.CustomerKey = "New Query Key32";
10    qd.Description = "Some Description";
11    qd.TargetUpdateType = "Overwrite";
12    qd.TargetType = "DE";
13    qd.QueryText = "Select EMAIL_ADDRESS as EMAIL from [Example DE]";
14    InteractionBaseObject ibo = new InteractionBaseObject();
15    ibo.CustomerKey = "FIRST SEND";
16    ibo.Name = "First Send";
17    qd.DataExtensionTarget = ibo;
18    CreateResult[] uoResults = framework.Create(new CreateOptions(), new APIObject[] { qd }, out requestID, out status);
19    Console.WriteLine("Status: " + status);
20    Console.WriteLine("Request ID: " + requestID);
21}

Sample SOAP Envelope 

1<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
2    <s:Header>
3    <a:Action s:mustUnderstand="1">Create</a:Action>
4    <a:MessageID>urn:uuid:ffac578d-73d8-46ac-b125-9cbf3952da25</a:MessageID>
5    <a:ReplyTo>
6    <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
7    </a:ReplyTo>
8    <a:To s:mustUnderstand="1">https://YOUR_SUBDOMAIN.soap.marketingcloudapis.com/Service.asmx</a:To>
9    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
10    <u:Timestamp u:Id="_0">
11    <u:Created>2009-12-01T23:29:41.590Z</u:Created>
12    <u:Expires>2009-12-01T23:34:41.590Z</u:Expires>
13    </u:Timestamp>
14    <o:UsernameToken u:Id="uuid-f356ce91-93e2-43bc-b890-b93bedb5dc47-1">
15    <o:Username>XXXXX</o:Username>
16    <o:Password>XXXXX</o:Password>
17    </o:UsernameToken>
18    </o:Security>
19    </s:Header>
20    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
21    <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
22    <Options>
23    </Options>
24    <Objects xsi:type="QueryDefinition">
25    <PartnerKey xsi:nil="true">
26    </PartnerKey>
27    <ObjectID xsi:nil="true">
28    </ObjectID>
29    <CustomerKey>New Query Key</CustomerKey>
30    <Name>New Query</Name>
31    <Description>Some Description</Description>
32    <QueryText>Select EMAIL_ADDRESS as EMAIL from [Example DE]</QueryText>
33    <TargetType>DE</TargetType>
34    <DataExtensionTarget>
35        <PartnerKey xsi:nil="true">
36        </PartnerKey>
37        <ObjectID xsi:nil="true">
38        </ObjectID>
39        <CustomerKey>FIRST SEND</CustomerKey>
40        <Name>First Send</Name>
41    </DataExtensionTarget>
42    <TargetUpdateType>Overwrite</TargetUpdateType>
43    </Objects>
44    </CreateRequest>
45    </s:Body>
46</s:Envelope>