I get the following Exception when I wrap my code snippet in a try catch. And this calls out the bug that fixed my issue with the following code
Needed to change how I cast my GUID to string
spurgoUserData.Add("azureactivedirectoryobjectid", systemUser["azureactivedirectoryobjectid"].ToString()); // do NOT cast GUID using (string)
Logs
spurgoUserData.Add("azureactivedirectoryobjectid", systemUser["azureactivedirectoryobjectid"].ToString()); // do NOT cast GUID using (string)
fetch system user data with idea02a959-0895-469f-ac88-bacbe0b96d11
system user entity result Dynamics.SpurGo.Gamification.DataModel.SystemUser
DONE fetch user data
getting data systemUser['spurgo_name'] = Greg Degruy
Exception System.InvalidCastException: Unable to cast object of type 'System.Guid' to type 'System.String'.
at Dynamics.SpurGo.Gamification.DataAccess.DynamicsDataAccess.GetSpurGoUserData()
Below I am working with the systemuser EntityType.
My fetch XML is grabbing system user information in my org, I key attribute I want to grab from the Entity response in this fetch is the azureactivedirectoryobjectidGuid attribute which is a Read Only Edm.Guid. It's going to be used in a bridge I am building to the Microsoft Graph API. When I query the Entity response from this fetch I seem to have not access to this attribute.
string xml = "<fetch distinct='false' version='1.0' output-format='xml-platform' mapping='logical' no-lock='true'>" +
"<entity name='systemuser'>" +
"<attribute name='azureactivedirectoryobjectid' />" +
"<attribute name='systemuserid' />" +
"<attribute name='fullname' />" +
"<attribute name='internalemailaddress' />" +
"<filter type='and'>" +
"<condition attribute='systemuserid' operator='eq' value='{" + userId.ToString() + "}' />" +
"<condition attribute='azureactivedirectoryobjectid' operator='not - null' />" +
"<condition attribute='internalemailaddress' operator='not-null' />" +
"<condition attribute='fullname' operator='not-null' />" +
"</filter>" +
"</entity>" +
"</fetch>";
The resulting fetch response as tested in the Fetch XML Tester using the XrmToolBox gives me the following.
<result>
<azureactivedirectoryobjectid>{2F3EAA47-FE7F-450F-97DE-00A03BA0F9C5}</azureactivedirectoryobjectid>
<systemuserid>{EA02A959-0895-469F-AC88-BACBE0B96D11}</systemuserid>
<fullname>Greg Degruy</fullname>
<internalemailaddress>[email protected]</internalemailaddress>
</result>
But in my C# plugin, I use the logic below and can not seem to access this attribute as seen in the trace logs logs. It even shows crmonln instead of grdegr as my alias.
Plugin trace logs
DONE fetch user data
user data for spurgo_name is Greg Degruy
user data for internalemailaddress is [email protected]
Code snippet from Plugin
tracingService.Trace("fetch spurgo user data");
Entity spurGoUser = RetrieveSpurGoUser(organizationService, executionContext.UserId);
tracingService.Trace("fetch system user data");
Entity systemUser = RetrieveSystemUser(organizationService, executionContext.UserId);
tracingService.Trace("DONE fetch user data");
if (spurGoUser != null && systemUser != null)
{
spurgoUserData = new Dictionary<string, dynamic>();
spurgoUserData.Add("spurgo_name", (string)spurGoUser["spurgo_name"]);
tracingService.Trace("user data spurgo_name " + spurgoUserData["spurgo_name"]);
spurgoUserData.Add("internalemailaddress", (string)systemUser["internalemailaddress"]);
tracingService.Trace("user data internalemailaddress " + systemUser["internalemailaddress"]);
spurgoUserData.Add("azureactivedirectoryobjectid", systemUser["azureactivedirectoryobjectid"].ToString());
tracingService.Trace("user data azureactivedirectoryobjectid " + spurgoUserData["azureactivedirectoryobjectid"]);
spurgoUserData.Add("fullname", (string)systemUser["fullname"]);
tracingService.Trace("user data " + spurgoUserData["fullname"]);
return spurgoUserData;
}
To cast a GUID from fetch XML you must use the following ToString() method.
spurgoUserData.Add("azureactivedirectoryobjectid", systemUser["azureactivedirectoryobjectid"].ToString());
Using an explicit cast will not work, like this.
spurgoUserData.Add("azureactivedirectoryobjectid", (string)systemUser["azureactivedirectoryobjectid"]);