Glen Mazza's Weblog

« Previous day (May 27, 2017) | Main | Next day (May 29, 2017) »

https://web-gmazza.rhcloud.com/blog/date/20170528 Sunday May 28, 2017

Using SoapUI to make Salesforce Marketing Cloud API calls

This tutorial shows how to make SOAP requests to Salesforce' Marketing Cloud API using the open source version of SmartBear's SoapUI. The feedback that SoapUI provides is very helpful in figuring out the correct SOAP request format for each action desired to run. SOAP request envelopes created within SoapUI can be easily saved for later reuse, allowing you to bypass the sometimes cumbersome Email Studio for often-run actions. Also, knowledge of the correct SOAP requests and SOAP responses returned helps speed up development for accessing the API via programmatic means, for example as shown in my Salesforce/Apache CXF tutorial. As with that tutorial, I'll be demonstrating creation of an email data folder, a simple task that provides an easy way to confirm connectivity and authentication before moving on to more complex activities.

Steps:

  1. Download SoapUI if you haven't already. Free open source version will do.

  2. Import the Marketing Cloud WSDL into a new SoapUI project. Select SoapUI menu item File | New SOAP Project. A New SOAP Project popup will appear (see illustration below.) Choose whatever project name desired. For the Initial WSDL, that can be retrieved from the Salesforce Marketing Cloud (SFMC) WSDL Links page. Choose the WSDL appropriate for the SF instance that you are using: S1, S4, S6, or S7 -- the illustration below shows S1 which may or may not be the correct one for your project. Contact SF customer support if you are uncertain which SFMC instance you are using.

    NewSOAPProject.png

    Good to check "Create Requests" as shown above, although this functionality is less useful here than for other web services. SFMC uses generic SOAP operations like Create and Retrieve that hold for dozens of objects instead of specific ones such as CreateXXX, RetrieveYYY, etc. resulting in unworkably large sample SOAP requests to handle the many objects these methods can be applied to.

  3. Configure Username Token Authentication for the SOAP calls. This need be configured only once for all SOAP calls. SoapUI provides efficient documentation for configuring WS-Security, of which SFMC uses the UsernameToken type. To configure, select menu item Project | Show Project View and select the WS-Security Configurations tab from the resulting popup. The (anonymized) settings below are what I configured for my own calls to work, make sure you also have the configuration as given in the bottom half of the illustration. The "Name" field at the top of the illustration can be whatever you want, it is used to identify the security properties desired for any particular SOAP call. The username and password can be what you use to log into Email Studio or can be a new account used specifically for SOAP calls.

    ConfigureWSS.png
  4. Create the SOAP request envelope. General process:

    1. Clone the sample request provided by SoapUI. Creating a clone allows you customize the sample SOAP call to something specific for your needs, while allowing the sample SOAP call to remain as-is so you can clone it again to perform a different action. To clone, right-click the sample request, select "Clone Request" and give the clone a useful name (say "CreateDataFolder"). You'll see it created next to the original sample request. Double-click the cloned request to bring it up in the editor window.

      CloneRequest.png
    2. Configure the UsernameToken for the call. In the editor window for the CreateDataFolder call, select the Auth button in the lower-left corner, choose Basic Authentication, leave the basic auth username and password blank, and for Outgoing WSS select the name of the UsernameToken authentication you configured earlier (see illustration).

      ConfigureWSSForRequest.png
    3. Check the SFMC documentation to help determine the required SOAP request format for the desired action. For each action, some trial and error can be expected to figure out the correct call. SoapUI excels at providing pre-call validation and rapid SOAP responses helpful for getting the calls right. The best first step is to check the documentation page for the specific SFMC object you're working with, in this case it would be the Data Folder page (note the other objects listed on the left-side menu in case you're working with something else.) You'll get a reasonably accurate listing of properties that can be defined and the types of actions (Create, Retrieve, etc.) permissible on the object. In the case of the Data Folder and some other objects, the bottom of the page gives a link to sample SOAP requests which I've not found to provide everything that's needed but a good starting step. In the case of creating data folders, I've found the description field to be required (or an unclear error response is given), the xmlns:xsi namespace needed to be added to the soap:envelope and a Client element for your project's MemberID needed. You'll also need to determine the ID of the parent folder for the new folder you're creating, one way to obtain this is to hover over the parent directory with the mouse in Email Studio and reading the "cid" value given in the tooltip popup at the bottom of your browser while doing so. The following is the format that worked for me to create the data folder:

      <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http://exacttarget.com/wsdl/partnerAPI">
          <soapenv:Body>
              <par:CreateRequest>
                  <par:Options/>
                  <par:Objects
                          xsi:type="par:DataFolder">
                      <par:Client>
                          <par:ClientID>...yourMID#...</par:ClientID>
                      </par:Client>
                      <par:ModifiedDate xsi:nil="true"/>
                      <par:ObjectID xsi:nil="true"/>
                      <par:ParentFolder>
                          <par:ModifiedDate xsi:nil="true"/>
                          <par:ID>...ParentFolderID#...</par:ID>
                          <par:ObjectID xsi:nil="true"/>
                      </par:ParentFolder>
                      <par:Name>emailFolderViaSoapUI</par:Name>
                      <par:Description>emailFolder1Desc</par:Description>
                      <par:ContentType>email</par:ContentType>
                      <par:IsActive>true</par:IsActive>
                      <par:IsEditable>true</par:IsEditable>
                      <par:AllowChildren>true</par:AllowChildren>
                  </par:Objects>
              </par:CreateRequest>
          </soapenv:Body>
      </soapenv:Envelope>
      
    4. Validate the SOAP request prior to sending it. If the SOAP request is not valid (missing required elements for example), running it will return a "400 Bad Request" message in the response window. These types of errors can normally be trapped beforehand by right-clicking the SOAP request in the editor window and choosing "Validate". Soap UI will return any WSDL or XML Schema validation errors for you to fix.

  5. Make the SOAP call. Clicking the green arrow in the upper-left corner of the editor window is all that is needed. However, even with the validation done earlier, the SOAP response may return an error message indicating further tweaking needed.

    • Check the SOAP response for any useful information. In the case of a create data folder request, you'll receive the "NewID" of the folder you created, which you can use to make further changes to the folder. Below shows the (anonymized) SOAP response to my SOAP request:

      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
          <soap:Header>
              <wsa:Action>CreateResponse</wsa:Action>
              <wsa:MessageID>urn:uuid:xxxxxx</wsa:MessageID>
              <wsa:RelatesTo>urn:uuid:yyyyyy</wsa:RelatesTo>
              <wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
              <wsse:Security>
                  <wsu:Timestamp wsu:Id="Timestamp-zzzzzz">
                      <wsu:Created>2017-05-21T14:19:52Z</wsu:Created>
                      <wsu:Expires>2017-05-21T14:24:52Z</wsu:Expires>
                  </wsu:Timestamp>
              </wsse:Security>
          </soap:Header>
          <soap:Body>
              <CreateResponse xmlns="http://exacttarget.com/wsdl/partnerAPI">
                  <Results>
                      <StatusCode>OK</StatusCode>
                      <StatusMessage>Folder created successfully.</StatusMessage>
                      <OrdinalID>0</OrdinalID>
                      <NewID>012345</NewID>
                      <NewObjectID>zzzzzz</NewObjectID>
                  </Results>
                  <RequestID>wwwwww</RequestID>
                  <OverallStatus>OK</OverallStatus>
              </CreateResponse>
          </soap:Body>
      </soap:Envelope>
      
    • Go into SFMC Email Studio to confirm the folder you created is there. If you had Email Studio already open, may need to refresh once or twice for the folder to appear. Can delete the folder from within the Studio if desired (or create another web service call to do that as well.)

    • Save the project to retain your SOAP request envelopes. Menu item Project | Save Project will do. Keeping this SOAP request template will allow you to tweak and reuse it later to create other folders.


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.