public override Guid WriteToCDS(OrganizationServiceProxy _serviceProxy)
        {
            Guid patientDeviceId = Guid.Empty;

            HealthCDM.msemr_device addPatientDevice = new HealthCDM.msemr_device();

            addPatientDevice.msemr_Patient        = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PatientId));
            addPatientDevice.msemr_DeviceNumber   = DeviceNumber;
            addPatientDevice.msemr_Version        = Version;
            addPatientDevice.msemr_Model          = Model;
            addPatientDevice.msemr_ExpirationDate = ExpirationDate;
            addPatientDevice.msemr_name           = Name;
            addPatientDevice.msemr_DeviceStatus   = new OptionSetValue(DeviceStatus);

            try
            {
                patientDeviceId = _serviceProxy.Create(addPatientDevice);

                if (patientDeviceId != Guid.Empty)
                {
                    DeviceId = patientDeviceId.ToString();
                    Console.WriteLine("Created Device [" + DeviceId + "] for Patient [" + PatientId + "]");
                }
                else
                {
                    throw new Exception("DeviceId == null");
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return(patientDeviceId);
        }
        public override Guid WriteToCDS(string cdsUrl, string cdsUserName, string cdsPassword)
        {
            Guid patientDeviceId = Guid.Empty;

            try
            {
                #region Get our Login Information

                // setup the variables
                OrganizationServiceProxy _serviceProxy;

                // homeRealmUri will stay null for now
                Uri homeRealmUri = null;

                // setup credentials from whatever is in the app.config
                ClientCredentials credentials;

                // same for organizationuri comes from app.config
                Uri organizationUri;

                // set the organization uri from what was in the app.config
                organizationUri = new Uri(cdsUrl);

                credentials = new ClientCredentials();
                credentials.UserName.UserName = cdsUserName;
                credentials.UserName.Password = cdsPassword;

                #endregion

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                using (_serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null))
                {
                    // To impersonate set the GUID of CRM user here (which I merely took from CRM itself
                    // would need not to use this caller id in the future (as it will change per instance of CRM)
                    //_serviceProxy.CallerId = new Guid("14D40CB7-81D5-E311-93F5-00155D00330C");
                    _serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());

                    //enable using proxy types
                    _serviceProxy.EnableProxyTypes();

                    HealthCDM.msemr_device addPatientDevice = new HealthCDM.msemr_device();

                    addPatientDevice.msemr_Patient        = new EntityReference(HealthCDM.Contact.EntityLogicalName, Guid.Parse(PatientId));
                    addPatientDevice.msemr_DeviceNumber   = DeviceNumber;
                    addPatientDevice.msemr_Version        = Version;
                    addPatientDevice.msemr_Model          = Model;
                    addPatientDevice.msemr_ExpirationDate = ExpirationDate;
                    addPatientDevice.msemr_name           = Name;
                    addPatientDevice.msemr_DeviceStatus   = new OptionSetValue(DeviceStatus);

                    try
                    {
                        patientDeviceId = _serviceProxy.Create(addPatientDevice);

                        if (patientDeviceId != Guid.Empty)
                        {
                            DeviceId = patientDeviceId.ToString();
                            Console.WriteLine("Created Device [" + DeviceId + "] for Patient [" + PatientId + "]");
                        }
                        else
                        {
                            throw new Exception("DeviceId == null");
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ex.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

            return(patientDeviceId);
        }