public void RetrieveEducations()
        {
            OrcidClient client = new OrcidClient(Properties.Settings.Default.OrcidAPIBaseUrl, Properties.Settings.Default.UserAuthorizatoinToken, Properties.Settings.Default.UserOrcidId);

            record userRecord = client.GetUserRecord();

            Assert.IsNotNull(userRecord);
            employments userEmployments = client.GetUserOrcidData <employments>(userRecord.activitiessummary.employments.path);

            Assert.IsNotNull(userEmployments);
        }
        public ActionResult RetrieveOrcidProfile()
        {
            // if the user is anonymus or null, redirect them to the signin page

            if (!InitializeOrcidController())
            {
                // TODO give a better error around not already being signed in.
                return(Redirect("/SignIn/"));
            }
            if (string.IsNullOrEmpty(UserAuthorizationToken))
            {
                // user authorization token is not defined
            }

            record userRecord = OrcidClient.GetUserRecord();

            // Create the entity contact to update.
            Contact contact = new Contact();

            contact.Id = xrmUser.ContactId.Id;
            contact.rp2_OricdBiography = userRecord?.person?.biography?.content;

            service = HttpContext.GetOrganizationService();

            service.Update(contact);

            if (userRecord.activitiessummary == null)
            {
                return(Redirect("/profile/orcid/"));
            }
            ExecuteMultipleRequest mRequest = new ExecuteMultipleRequest();

            mRequest.Settings = new ExecuteMultipleSettings();
            mRequest.Settings.ContinueOnError = true;
            mRequest.Settings.ReturnResponses = false;
            mRequest.Requests = new OrganizationRequestCollection();

            employments userEmployments = OrcidClient.GetUserOrcidData <employments>(userRecord.activitiessummary.employments.path);

            if (userEmployments?.employmentsummary != null)
            {
                foreach (employmentsummary es in userEmployments.employmentsummary)
                {
                    rp2_employment employment = new rp2_employment("rp2_sourceidentifier", es.path);

                    employment.rp2_Person               = xrmUser.ContactId;
                    employment.rp2_Department           = es?.departmentname;
                    employment.rp2_OrganizationNameText = es?.organization?.name;
                    employment.rp2_RoleTitle            = es.roletitle;
                    employment.rp2_City          = es.organization?.address?.city;
                    employment.rp2_Country       = es.organization?.address?.country.ToString();
                    employment.rp2_StateProvince = es.organization?.address?.region;
                    employment.rp2_EndDate       = es.enddate?.ToDateTime();
                    employment.rp2_StartDate     = es.startdate?.ToDateTime();

                    //service.Create(employment);
                    UpsertRequest cRq = new UpsertRequest();
                    cRq.Target = employment;
                    mRequest.Requests.Add(cRq);
                }
            }
            if (mRequest.Requests.Count > 0)
            {
                ExecuteMultipleResponse response = service.Execute(mRequest) as ExecuteMultipleResponse;
                // TODO validate resonse
                var errorFaults = response.Responses.Where(r => r.Fault != null);
                if (errorFaults.Any())
                {
                    string errorMessages = "{" + string.Join("}, {", errorFaults.Select(f => f.Fault.Message)) + "}";
                    throw new Exception(errorMessages);
                }
            }
            return(Redirect("/profile/orcid/"));
        }