Esempio n. 1
0
        public async Task <JsonResult> CreateAccount([FromBody] ViewModels.Account item)
        {
            // create a new account
            Contexts.Microsoft.Dynamics.CRM.Account account = new Contexts.Microsoft.Dynamics.CRM.Account();

            DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Account> AccountCollection = new DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Account>(_system);
            DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Contact> ContactCollection = new DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Contact>(_system);

            AccountCollection.Add(account);
            account.Name        = item.name;
            account.Description = item.description;

            if (item.primarycontact != null)
            {
                // get the contact.
                Contexts.Microsoft.Dynamics.CRM.Contact contact = new Contexts.Microsoft.Dynamics.CRM.Contact();
                contact.Fullname         = item.primarycontact.name;
                contact.Contactid        = new Guid(item.primarycontact.id);
                account.Primarycontactid = contact;
            }


            await _system.SaveChangesAsync(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset);

            // if we have not yet authenticated, then this is the new record for the user.
            string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

            if (userSettings.IsNewUserRegistration)
            {
                // we can now authenticate.
                if (userSettings.AuthenticatedUser == null)
                {
                    Models.User user = new Models.User();
                    user.Active   = true;
                    user.Guid     = userSettings.ContactId;
                    user.SmUserId = userSettings.UserId;
                    userSettings.AuthenticatedUser = user;
                }

                userSettings.IsNewUserRegistration = false;

                string userSettingsString = JsonConvert.SerializeObject(userSettings);
                // add the user to the session.
                _httpContextAccessor.HttpContext.Session.SetString("UserSettings", userSettingsString);
            }

            return(Json(account));
        }
Esempio n. 2
0
        public async Task <JsonResult> GetDynamicsApplications()
        {
            // create a DataServiceCollection to add the record
            DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Adoxio_application> ApplicationCollection = new DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Adoxio_application>(_system);

            // get all applications in Dynamics filtered by the applying person
            //var dynamicsApplicationList = await _system.Adoxio_applications.AddQueryOption("$filter", "_adoxio_applyingperson_value eq 7d4a5b20-e352-e811-8140-480fcfeac941").ExecuteAsync();

            // get all applications in Dynamics
            var dynamicsApplicationList = await _system.Adoxio_applications.ExecuteAsync();

            List <ViewModels.AdoxioApplication> adoxioApplications = new List <AdoxioApplication>();

            ViewModels.AdoxioApplication adoxioApplication = null;

            if (dynamicsApplicationList != null)
            {
                foreach (var dynamicsApplication in dynamicsApplicationList)
                {
                    adoxioApplication      = new ViewModels.AdoxioApplication();
                    adoxioApplication.name = dynamicsApplication.Adoxio_name;
                    //adoxioApplication.applyingPerson = dynamicsApplication.Adoxio_ApplyingPerson.Adoxio_contact_adoxio_application_ApplyingPerson.ToString();
                    Guid?applyingPersonId = dynamicsApplication._adoxio_applyingperson_value;

                    if (applyingPersonId != null)
                    {
                        // fetch a contact
                        Contexts.Microsoft.Dynamics.CRM.Contact contact = await _system.Contacts.ByKey(contactid : applyingPersonId).GetValueAsync();

                        adoxioApplication.applyingPerson = contact.Fullname;
                    }

                    adoxioApplication.jobNumber = dynamicsApplication.Adoxio_jobnumber;

                    Guid?adoxio_licencetypeId = dynamicsApplication._adoxio_licencetype_value;
                    if (adoxio_licencetypeId != null)
                    {
                        Adoxio_licencetype adoxio_licencetype = await _system.Adoxio_licencetypes.ByKey(adoxio_licencetypeid : adoxio_licencetypeId).GetValueAsync();

                        adoxioApplication.licenseType = adoxio_licencetype.Adoxio_name;
                    }

                    adoxioApplications.Add(adoxioApplication);
                }
            }

            return(Json(adoxioApplications));
        }
Esempio n. 3
0
        public async Task <JsonResult> CreateContact([FromBody] ViewModels.Contact viewModel)
        {
            Contexts.Microsoft.Dynamics.CRM.Contact item = viewModel.ToModel();

            // create a new contact.
            Contexts.Microsoft.Dynamics.CRM.Contact contact = new Contexts.Microsoft.Dynamics.CRM.Contact();

            // create a DataServiceCollection to add the record
            DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Contact> ContactCollection = new DataServiceCollection <Contexts.Microsoft.Dynamics.CRM.Contact>(_system);

            // add a new contact.
            ContactCollection.Add(contact);

            // changes need to made after the add in order for them to be saved.
            contact.CopyValues(item);
            contact.Contactid = Guid.NewGuid();

            // PostOnlySetProperties is used so that settings such as owner will get set properly by the dynamics server.

            await _system.SaveChangesAsync(SaveChangesOptions.PostOnlySetProperties | SaveChangesOptions.BatchWithSingleChangeset);

            // if we have not yet authenticated, then this is the new record for the user.
            string       temp         = _httpContextAccessor.HttpContext.Session.GetString("UserSettings");
            UserSettings userSettings = JsonConvert.DeserializeObject <UserSettings>(temp);

            if (userSettings.IsNewUserRegistration)
            {
                if (string.IsNullOrEmpty(userSettings.ContactId))
                {
                    userSettings.ContactId = contact.Contactid.ToString();
                    string userSettingsString = JsonConvert.SerializeObject(userSettings);
                    // add the user to the session.
                    _httpContextAccessor.HttpContext.Session.SetString("UserSettings", userSettingsString);
                }
            }

            return(Json(contact));
        }