How to Retrieve an Owner Lookup in Dynamics 365 with the WebAPI

Adam Murchison, 02 April 2020

There’s an example on the WebAPI documentation of how to retrieve a related entity and their columns. As I was following this documentation to retrieve an owner lookup, I unexpectedly got an error and then did some digging to see why this failed.

Once you know how to retrieve the owner of a record, regardless of it being a team or a user, it will seem obvious but compared to a standard lookup it is slightly different.

Here’s the code to retrieve an owner of a record. For sake of example, I’m just retrieving the id and the name of the record but you can add your own columns in the expand section of the method.

Xrm.WebApi.online.retrieveRecord("account", "a9e49c4a-c3c4-e900-b97c-000g3cf1b934", 
     "?$expand=owninguser($select=systemuserid, fullname), owningteam($select=teamid, name)").
     then( 

     function (account) { 
          var owningTeam = account.owningteam; 
          var owningUser = account.owninguser; 

          if (owningUser != null) { 
               var ownerId = owningUser.ownerid; 
               var ownerName = owningUser.fullname; 
               var owner = [{ entityType: "systemuser", id: ownerId, name: ownerName }] 
          } 
          else if (owningTeam != null) { 
               var ownerId = owningTeam.ownerid; 
               var ownerName = owningTeam.name; 
               var owner = [{ entityType: "team", id: ownerId, name: ownerName }]   
          } 
     }, 

     function (e) {                      
          console.log(e.message); 
          alert(e.message); 
     }); 

Here you can see I’m not selecting any rows from the account but am expanding rows on the ‘owninguser’ and the ‘owningteam’ entities. This is how you retrieve an owner of a record regardless if it’s a user or a team, this is technically retrieving both values but only one will be populated and the other will be null, you can see how I handle this in the success callback of the retrieve function.

I hope this makes retrieving the owner of a record using the WebAPI nice and simple for you!