Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • shinsuke nakayama 109 posts 250 karma points
    Mar 15, 2017 @ 04:46
    shinsuke nakayama
    0

    RecordService - For logged in Member, the user can't insert new record and trigger the workflow

    Hi guys,

    I can't seem to add new records to the Umbraco Form using the RecordService API. I couldn't find much documentation but I based my code from the following forum.

    https://our.umbraco.org/forum/umbraco-pro/contour/60891-Add-fields-programmatically-Attach-custom-data

    This works if the user is not logged in, but if logged in, get following error: on this line

    rs2.Submit(record, form);
    

    on Umbraco Form 4.4.0

    Message: An item with the same key has already been added.
    
       at Umbraco.Core.Cache.HttpRuntimeCacheProvider.GetCacheItem(String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, CacheDependency dependency)
       at Umbraco.Core.Cache.HttpRuntimeCacheProvider.GetCacheItem(String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, String[] dependentFiles)
       at Umbraco.Core.Cache.DeepCloneRuntimeCacheProvider.GetCacheItem(String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, String[] dependentFiles)
       at Umbraco.Forms.Core.Cache.CacheProviderExtensions.GetCacheItem[T](IRuntimeCacheProvider provider, String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, String[] dependentFiles)
       at Umbraco.Forms.Data.StringHelper.ParseMemberPlaceholders(String value, Object memberKey)
       at Umbraco.Forms.Data.StringHelper.ParsePlaceHolders(HttpContext context, Record record, String value)
       at Umbraco.Forms.Data.StringHelper.ParsePlaceHolders(Record record, String value)
       at Umbraco.Forms.Core.Services.WorkflowService.ExecuteWorkflows(List`1 workflows, RecordEventArgs e)
       at Umbraco.Forms.Core.Services.WorkflowService.ExecuteWorkflows(Record record, Form form, FormState state, Boolean editMode)
       at Umbraco.Forms.Web.Services.RecordService.Submit(Record record, Form form)
       at Test_Web.Controllers.ApiController.EnquiryApiController.HandleEnquiry(CustomerEnquiryModel model) in c:\Development\Test_Web\Controllers\ApiController\EnquiryApiController.cs:line 46
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
    

    on Umbraco Forms 4.3.2

    Message: Object reference not set to an instance of an object.
    
       at Umbraco.Core.Cache.HttpRuntimeCacheProvider.GetCacheItem(String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, CacheDependency dependency)
       at Umbraco.Core.Cache.HttpRuntimeCacheProvider.GetCacheItem(String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, String[] dependentFiles)
       at Umbraco.Core.Cache.DeepCloneRuntimeCacheProvider.GetCacheItem(String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, String[] dependentFiles)
       at Umbraco.Forms.Core.Cache.CacheProviderExtensions.GetCacheItem[T](IRuntimeCacheProvider provider, String cacheKey, Func`1 getCacheItem, Nullable`1 timeout, Boolean isSliding, CacheItemPriority priority, CacheItemRemovedCallback removedCallback, String[] dependentFiles)
       at Umbraco.Forms.Data.StringHelper.ParseMemberPlaceholders(String value, Object memberKey)
       at Umbraco.Forms.Data.StringHelper.ParsePlaceHolders(HttpContext context, Record record, String value)
       at Umbraco.Forms.Data.StringHelper.ParsePlaceHolders(Record record, String value)
       at Umbraco.Forms.Core.Services.WorkflowService.ExecuteWorkflows(List`1 workflows, RecordEventArgs e)
       at Umbraco.Forms.Core.Services.WorkflowService.ExecuteWorkflows(Record record, Form form, FormState state, Boolean editMode)
       at Umbraco.Forms.Web.Services.RecordService.Submit(Record record, Form form)
       at Test_Web.Controllers.ApiController.EnquiryApiController.HandleEnquiry(CustomerEnquiryModel model) in c:\Development\Test_Web\Controllers\ApiController\EnquiryApiController.cs:line 46
       at lambda_method(Closure , Object , Object[] )
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
       at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
    

    my code looks like this:

    private string _formId = ConfigurationManager.AppSettings["messageBoxId"]; //"b87a8454-6f02-43c0-8ffa-3938f0fa9a66";
    public HttpResponseMessage HandleEnquiry(CustomerEnquiryModel model)
            {
                using (var formStorage = new FormStorage())
                {
                    var form = formStorage.GetForm(new Guid(_formId));
                    var record = new Record();
    
                    foreach (var f in form.AllFields)
                    {
                        SetValueToForm("name", WebUtility.HtmlEncode(model.Name), f, record);
                        SetValueToForm("email", WebUtility.HtmlEncode(model.Email), f, record);
                        SetValueToForm("companyName", WebUtility.HtmlEncode(model.CompanyName), f, record);
                        SetValueToForm("enquiryType", WebUtility.HtmlEncode(model.EnquiryType), f, record);
                        SetValueToForm("subject", WebUtility.HtmlEncode(model.Subject), f, record);
                        SetValueToForm("comment", WebUtility.HtmlEncode(model.Comment), f, record);
                    }
    
                    record.RecordData = record.GenerateRecordDataAsJson();
                    record.Form = new Guid(_formId);
                    record.IP = GetIP();
                    record.UmbracoPageId = 1084;
                    //record.State = FormState.Submitted;
    
                    //var rs = new RecordStorage();
                    //rs.InsertRecord(record, form);
    
                    var rs2 = new RecordService();
                    rs2.Submit(record, form);
    
                    var result = new SimpleApiResponseModel { Result = true };
    
                    return new HttpResponseMessage()
                    {
                        Content = new JsonContent(result)
                    };
                }
            }
    
            private static void SetValueToForm(string label, string value, Field f, Record record)
            {
                if (f.Alias == label && value != null)
                {
                    var key = Guid.NewGuid();
                    var recordField = new RecordField()
                    {
                        Alias = f.Alias,
                        FieldId = f.Id,
                        Field = f,
                        Key = key,
                        Record = record.Id,
                        Values = new List<object>() {value}
                    };
    
                    var recordFieldStorage = new RecordFieldStorage();
                    recordFieldStorage.InsertRecordField(recordField);
                    record.RecordFields.Add(key, recordField);
                }
            }
    

    Am I missing something? Or is this a bug in Umbraco Form?

  • shinsuke nakayama 109 posts 250 karma points
    Mar 20, 2017 @ 02:07
Please Sign in or register to post replies

Write your reply to:

Draft