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

            record userRecord = client.GetUserRecord();

            Assert.IsNotNull(userRecord);
        }
        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 Index()
        {
            // 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 the Orcid code parameter does not exist, re-direct the user back to the profile page.
            if (!Request.QueryString.AllKeys.Contains("code"))
            {
                // TODO give a better error around not already being signed in.
                return(Redirect("/profile/"));
            }
            string orcidReturnCode = Request.QueryString["code"];

            OrcidAccessTokenDetails accessToken = OrcidClient.GetAccessToken(OrcidOAuthUrl, orcidReturnCode, OrcidClientSecret, OrcidClientId, OrcidClientRequestUri);

            // Create the entity contact to update.
            Entity contact = new Entity(xrmUser.ContactId.LogicalName);

            contact.Id = xrmUser.ContactId.Id;

            contact["rp2_orcid"] = accessToken.OrcidId;
            DateTime utcExpiry = DateTime.UtcNow.AddSeconds(Convert.ToInt32(accessToken.TokenExpiryDate));

            contact["rp2_orcidutcexpiry"]   = utcExpiry;
            contact["rp2_orcidaccesstoken"] = accessToken.AccessToken;
            contact["rp2_orcid"]            = accessToken.OrcidId;


            UserAuthorizationToken = accessToken.AccessToken;
            UserOrcid   = accessToken.OrcidId;
            OrcidClient = new OrcidClient(OrcidApiBaseUrl, UserAuthorizationToken, UserOrcid);

            record record = OrcidClient.GetUserRecord();

            if (string.IsNullOrEmpty(xrmUser.FirstName) && !string.IsNullOrEmpty(record?.person?.name?.givennames?.Value))
            {
                contact["firstname"] = record.person.name.givennames.Value;
            }
            if (string.IsNullOrEmpty(xrmUser.LastName) && !string.IsNullOrEmpty(record?.person?.name?.familyname?.Value))
            {
                contact["lastname"] = record.person.name.familyname.Value;
            }

            service.Update(contact);

            return(Redirect("/profile/"));
        }
        public bool InitializeOrcidController()
        {
            // Get the CRM user from the xrm Portal HttpContext
            xrmUser = HttpContext.GetUser();
            if (xrmUser == null || xrmUser == CrmUser.Anonymous)
            {
                return(false);
            }


            OrcidOAuthUrl = HttpContext.GetWebsite().Settings.Get <string>(orcidOAuthUrlKey);
            if (OrcidOAuthUrl.EndsWith("/"))
            {
                OrcidOAuthUrl = OrcidOAuthUrl.Substring(0, OrcidOAuthUrl.Length - 1);
            }

            OrcidClientId         = HttpContext.GetWebsite().Settings.Get <string>(orcidClientIdKey);
            OrcidClientSecret     = HttpContext.GetWebsite().Settings.Get <string>(orcidClientSecretKey);
            OrcidClientRequestUri = Request.Url.GetLeftPart(UriPartial.Authority) + "/ResearchPortal/Orcid";

            OrcidApiBaseUrl = HttpContext.GetWebsite().Settings.Get <string>(orcidApiKey);


            service = HttpContext.GetOrganizationService();

            Entity userContact = service.Retrieve(xrmUser.ContactId.LogicalName, xrmUser.ContactId.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet("rp2_orcid", "rp2_orcidaccesstoken"));

            if (userContact.Contains("rp2_orcid"))
            {
                UserOrcid = userContact["rp2_orcid"].ToString();
            }

            if (userContact.Contains("rp2_orcidaccesstoken"))
            {
                UserAuthorizationToken = userContact["rp2_orcidaccesstoken"].ToString();
            }


            OrcidClient = new OrcidClient(OrcidApiBaseUrl, UserAuthorizationToken, UserOrcid);
            return(true);
        }