public void ApiCreateDefinedValue(string apiKey, Rock.Core.DTO.DefinedValue DefinedValue) { using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { Rock.CMS.UserService userService = new Rock.CMS.UserService(); Rock.CMS.User user = userService.Queryable().Where(u => u.ApiKey == apiKey).FirstOrDefault(); if (user != null) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.DefinedValueService DefinedValueService = new Rock.Core.DefinedValueService(); Rock.Core.DefinedValue existingDefinedValue = new Rock.Core.DefinedValue(); DefinedValueService.Add(existingDefinedValue, user.PersonId); uow.objectContext.Entry(existingDefinedValue).CurrentValues.SetValues(DefinedValue); if (existingDefinedValue.IsValid) { DefinedValueService.Save(existingDefinedValue, user.PersonId); } else { throw new WebFaultException <string>(existingDefinedValue.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest); } } else { throw new WebFaultException <string>("Invalid API Key", System.Net.HttpStatusCode.Forbidden); } } }
public void CreateDefinedValue(Rock.Core.DTO.DefinedValue DefinedValue) { var currentUser = Rock.CMS.UserService.GetCurrentUser(); if (currentUser == null) { throw new WebFaultException <string>("Must be logged in", System.Net.HttpStatusCode.Forbidden); } using (Rock.Data.UnitOfWorkScope uow = new Rock.Data.UnitOfWorkScope()) { uow.objectContext.Configuration.ProxyCreationEnabled = false; Rock.Core.DefinedValueService DefinedValueService = new Rock.Core.DefinedValueService(); Rock.Core.DefinedValue existingDefinedValue = new Rock.Core.DefinedValue(); DefinedValueService.Add(existingDefinedValue, currentUser.PersonId); uow.objectContext.Entry(existingDefinedValue).CurrentValues.SetValues(DefinedValue); if (existingDefinedValue.IsValid) { DefinedValueService.Save(existingDefinedValue, currentUser.PersonId); } else { throw new WebFaultException <string>(existingDefinedValue.ValidationResults.AsDelimited(", "), System.Net.HttpStatusCode.BadRequest); } } }