Get all the Roles + Permissions defined in a Marketo instance

Ever want to export all your Role definitions from Admin » Users and Roles? Sure comes in handy as a backup and/or when setting up other instances.

This hack is near madness. But it works, at least for now: it’s reverse-engineered from the legacy UI, so if Admin ever gets forcibly moved to Sky, it’ll have to be redone.

Just go to Users and Roles, open up your browser’s F12 Console and paste this in. It only gets one Role by default, as a sample. Once you verify at the downloaded JSON, set getJustOne = false and run it again to get all of them. (N.B. Only tested in Firefox 50-something and Chrome 60-something, as something like this doesn’t have to be cross-browser... and also left nested Promises that I’d usually refactor.)

/* set to `false` after testing! */
var getJustOne = true;

var exportedProps = [
  "children",
  "roleId",
  "roleName",
  "roleDescription",
  "rolePerms",
  "text",
  "checked",
  "accessLevel"
];

var commonFetchOptions = {
   method : "POST",
   credentials : "same-origin",
   headers : {
      "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8",
      "X-Requested-With" : "XMLHttpRequest"
   }
};

fetch("/custAdmin/getAllRoles", 
  Object.assign( 
    commonFetchOptions, 
    { body: "start=0&limit=100&pageSize=100&xsrfId=" + MktSecurity.getXsrfId() } 
  ) )
  .then( resp => resp.json() )
  .then( respJ => 
     Promise.all( 
       respJ.data
		 .slice(-getJustOne)
		 .map( role => fetch("/custAdmin/editRole?cadRoleId=" + role.id,     
           Object.assign( 
             commonFetchOptions, 
             { body: "ajaxHandler=MktSession&mktReqUid=1587774079402%3A2845&xsrfId=" + MktSecurity.getXsrfId() } 
           ) ) 
         )
     )
     .then( allResps => 
        Promise.all( 
          allResps.map( resp => resp.json() ) 
        )
        .then( allRespJ => allRespJ.map( respJ => respJ.JSONResults.appvars ) )
        .then( trimmedRespJ => { 
			const downloadable = JSON.stringify(trimmedRespJ, exportedProps, 2);
			const downloader = document.createElement("a");
			downloader.href = window.URL.createObjectURL( 
              new Blob( [downloadable],{ type:"application/json" } ) 
            );
			downloader.download = "roles.json";
			document.body.appendChild(downloader);
			downloader.click();
		} )
     )
  )

JSON output looks like so: