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

private void button12_Click(object sender, EventArgs e){
    SoapClient framework = new SoapClient();
    framework.ClientCredentials.UserName.UserName = Properties.Settings.Default.UN_S1;
    framework.ClientCredentials.UserName.Password = Properties.Settings.Default.PW_S1;
    String requestID;
    String status;
    QueryDefinition qd = new QueryDefinition();
    qd.Name = "New Query333";
    qd.CustomerKey = "New Query Key32";
    qd.Description = "Some Description";
    qd.TargetUpdateType = "Overwrite";
    qd.TargetType = "DE";
    qd.QueryText = "Select EMAIL_ADDRESS as EMAIL from [Example DE]";
    InteractionBaseObject ibo = new InteractionBaseObject();
    ibo.CustomerKey = "FIRST SEND";
    ibo.Name = "First Send";
    qd.DataExtensionTarget = ibo;
    CreateResult[] uoResults = framework.Create(new CreateOptions(), new APIObject[] { qd }, out requestID, out status);
    Console.WriteLine("Status: " + status);
    Console.WriteLine("Request ID: " + requestID);
}

Sample SOAP Envelope

<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">
    <s:Header>
    <a:Action s:mustUnderstand="1">Create</a:Action>
    <a:MessageID>urn:uuid:ffac578d-73d8-46ac-b125-9cbf3952da25</a:MessageID>
    <a:ReplyTo>
    <a:Address>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</a:Address>
    </a:ReplyTo>
    <a:To s:mustUnderstand="1">https://YOUR_SUBDOMAIN.soap.marketingcloudapis.com/Service.asmx</a:To>
    <o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <u:Timestamp u:Id="_0">
    <u:Created>2009-12-01T23:29:41.590Z</u:Created>
    <u:Expires>2009-12-01T23:34:41.590Z</u:Expires>
    </u:Timestamp>
    <o:UsernameToken u:Id="uuid-f356ce91-93e2-43bc-b890-b93bedb5dc47-1">
    <o:Username>XXXXX</o:Username>
    <o:Password>XXXXX</o:Password>
    </o:UsernameToken>
    </o:Security>
    </s:Header>
    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <CreateRequest xmlns="http://exacttarget.com/wsdl/partnerAPI">
    <Options>
    </Options>
    <Objects xsi:type="QueryDefinition">
    <PartnerKey xsi:nil="true">
    </PartnerKey>
    <ObjectID xsi:nil="true">
    </ObjectID>
    <CustomerKey>New Query Key</CustomerKey>
    <Name>New Query</Name>
    <Description>Some Description</Description>
    <QueryText>Select EMAIL_ADDRESS as EMAIL from [Example DE]</QueryText>
    <TargetType>DE</TargetType>
    <DataExtensionTarget>
        <PartnerKey xsi:nil="true">
        </PartnerKey>
        <ObjectID xsi:nil="true">
        </ObjectID>
        <CustomerKey>FIRST SEND</CustomerKey>
        <Name>First Send</Name>
    </DataExtensionTarget>
    <TargetUpdateType>Overwrite</TargetUpdateType>
    </Objects>
    </CreateRequest>
    </s:Body>
</s:Envelope>