Expand my Community achievements bar.

Announcing the launch of new sub-community for Campaign Web UI to cater specifically to the needs of Campaign Web UI users!

AC Tips & Tricks: Custom API - Expose data in JSON (Part 1)

Avatar

Community Advisor

10/25/22

Alternative approach for exposing campaign data through a custom jssp API by leveraging "Dynamic JavaScript Pages".

 

  1. Navigate to "Administration/Configuration/Dynamic JavaScript Pages"
  2. Create a new page and set a custom name in the following format (namespace:pagename.jssp) i.e. "cus:recipientAPI.jssp"
  3. Paste the below script:

Script

 

 

<%@ page import="/nl/core/shared/nl.js"%>
<%  

//required server-side dependencies
NL.ns('NL.API');
NL.require('/nl/core/shared/core.js')
  .require('/nl/core/jsspcontext.js');

//response headers
response.setContentType("application/json");
response.addHeader("Access-Control-Allow-Origin", "*")
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Date", new Date());    
response.addHeader("Expires", new Date());        

          //require authentication
          var jsspContext = new NL.JSSPContext(request);
                
            if( !jsspContext.checkAuthentication() ) {              
              response.sendError(403, "Authentication required");
              return;              
            } else if(request['method'] != 'POST'){ //API only accepts requests made through POST
               response.sendError(405, "Method Not Allowed ["+request['method']+"]");
              return; 
             }                         

//fetch url param id value      
var rcpId = request.getParameter('id'); 

try {   
       
    if (rcpId){
    
               var rcpObj = nms.recipient.load(rcpId); //load recipient
               
               //construct response array, add/remove properties as required from recipient schema                 
               var response = {
                  'id':rcpObj.id,
                  'firstName':rcpObj.firstName,
                  'lastName':rcpObj.lastName,
                  'email':rcpObj.email,                     
                  'company':rcpObj.company,
                  'jobTitle':rcpObj.JOB_TITLE,
                  'phone':rcpObj.phone,
                  'origin':rcpObj.origin,
                  'doubleOptIn':rcpObj.doubleOptin,
                  'blacklist':rcpObj.blackList,
                  'address1':rcpObj.location.address1,
                  'address2':rcpObj.location.address2,
                  'address3':rcpObj.location.address3,
                  'address4':rcpObj.location.address4,
                  'zipCode':rcpObj.location.zipCode,
                  'city':rcpObj.location.city,
                  'lastModified':rcpObj.lastModified
                  }                  
                        
                  document.write(JSON.stringify(response)); //write response data to browser                       
    } else {   
     response.sendError(400, "Bad request, missing [id] param or value");
    }

                  
} catch (err) {//catch errors
  response = {'error':err.toString()};
  document.write(JSON.stringify(response));
  } 
            
%>

 

 

 Dependencies

The following libraries are mandatory to enforce/verify jssp authentication for security purposes

 

 

<%@ page import="/nl/core/shared/nl.js"%>
<%  
NL.ns('NL.API');
NL.require('/nl/core/shared/core.js')
  .require('/nl/core/jsspcontext.js');

 

 

Headers

Basic headers to control cache, see a list of configurable response headers in the following link https://developer.mozilla.org/en-US/docs/Glossary/Response_header

 

 

response.setContentType("application/json");
response.addHeader("Access-Control-Allow-Origin", "*")
response.addHeader("Pragma", "no-cache");
response.addHeader("Cache-Control", "no-cache");
response.addHeader("Date", new Date());    
response.addHeader("Expires", new Date());

 

 

 Authentication

You may control jssp authentication by using "logonEscalation(webapp)" or specify a custom operator, the following functions are available.

Request

  1. URL structure should be as following: https://<yourinstance>/cus/recipientAPI.jssp?id=1234567
  2. cus is the namespace set when creating the jssp page and id to contain the recipient id to load.
  3. method, to test the jssp on your browser, change line 23 (request['method'] != 'POST') to (request['method'] != 'GET'). otherwise, POST method only allows requests from forms or tools which allows to configure the method type.
  4. You must be logged in to adobe campaign instance as the jssp context relies on a session token, to do this, login to any webapp on the same instance, alternative use the following url structure and fill in your instance URL https://<yourinstance>/nl/jsp/logon.jsp?target=%2FwebApp%2F

David__Garcia_0-1666714967739.png

Link(s)

Part 2

The next blog (link to part 2)  will rely on the custom API to load data into a webapp form using AJAX.