public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string token = request.DataStore.GetJson("MsCrmToken")["access_token"].ToString(); AuthenticationHeaderValue bearer = new AuthenticationHeaderValue("Bearer", token); RestClient _rc = new RestClient(MsCrmEndpoints.ENDPOINT, bearer); string response = await _rc.Get(MsCrmEndpoints.URL_ORGANIZATIONS); MsCrmOrganization[] orgs = JsonConvert.DeserializeObject <MsCrmOrganization[]>(response); Task <string>[] resultsList = new Task <string> [orgs.Length]; for (int i = 0; i < orgs.Length; i++) { resultsList[i] = _rc.Get(MsCrmEndpoints.URL_ORGANIZATION_METADATA, $"organizationUrl={WebUtility.UrlEncode(orgs[i].OrganizationUrl)}"); } try { await Task.WhenAll(resultsList); } catch { // do nothing } finally { for (int i = 0; i < resultsList.Length; i++) { if (resultsList[i].IsFaulted && resultsList[i].Exception != null) { orgs[i].ErrorCategory = resultsList[i].Exception.Message.ToLowerInvariant().Contains("failed authorization") ? 1 : 2; orgs[i].ErrorCode = resultsList[i].Exception.HResult; orgs[i].ErrorMessage = resultsList[i].Exception.Message; } else { MsCrmOrganization o = JsonConvert.DeserializeObject <MsCrmOrganization>(resultsList[i].Result); orgs[i].ConnectorUrl = o.ConnectorUrl; if (string.IsNullOrEmpty(o.ConnectorUrl)) { request.Logger.LogEvent("MSCRM-NoConnectorURL", new System.Collections.Generic.Dictionary <string, string> { { o.OrganizationName, o.OrganizationId } }); } } } } // This is a bit of a dance to accomodate ActionResponse and its need for a JObject response = JsonConvert.SerializeObject(orgs); return(string.IsNullOrWhiteSpace(response) ? new ActionResponse(ActionStatus.Failure, new JObject(), "MsCrm_NoOrgs") : new ActionResponse(ActionStatus.Success, JsonUtility.GetJObjectFromStringValue(response))); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string token = request.DataStore.GetJson("MsCrmToken", "access_token"); MsCrmOrganization orgObject = new MsCrmOrganization() { OrganizationUrl = request.DataStore.GetValue("OrganizationUrl"), OrganizationId = request.DataStore.GetValue("OrganizationId") }; AuthenticationHeaderValue bearer = new AuthenticationHeaderValue("Bearer", token); RestClient rc = new RestClient(MsCrmEndpoints.ENDPOINT, bearer); try { string response = await rc.Get(MsCrmEndpoints.URL_ORGANIZATION_METADATA, $"organizationUrl={WebUtility.UrlEncode(orgObject.OrganizationUrl)}"); MsCrmOrganization orgDetails = JsonConvert.DeserializeObject <MsCrmOrganization>(response); orgObject.ConnectorUrl = orgDetails.ConnectorUrl; if (string.IsNullOrEmpty(orgObject.ConnectorUrl)) { request.Logger.LogEvent("MSCRM-NoConnectorURL", new System.Collections.Generic.Dictionary <string, string> { { orgObject.OrganizationName, orgObject.OrganizationId } }); } request.DataStore.AddToDataStore("ConnectorUrl", orgObject.ConnectorUrl, DataStoreType.Public); return(new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject())); } catch (Exception e) { if (e.Message.ToLowerInvariant().Contains("failed authorization")) { return(new ActionResponse(ActionStatus.Failure, null, e, "MsCrm_Unauthorized")); } return(new ActionResponse(ActionStatus.Failure, null, e, "MsCrm_MetadataError")); } }